Professional Documents
Culture Documents
Import Import: "Sample - TXT"
Import Import: "Sample - TXT"
Page 1 of 6
Main.java 01-11-2023 06:15
65 int i=1;
66
67 while((name = file.readLine()) != null)
68 {
69 System.out.println(name);
70 System.out.println("");
71 }
72
73 file.close();
74 }
75
76 catch(FileNotFoundException e)
77 {
78 System.out.println("\nERROR : File not Found !!!");
79 }
80 }
81
82 //addRecords method
83
84 public void searchRecords() throws IOException
85 {
86 try
87 {
88 BufferedReader file = new BufferedReader(new FileReader("sample.txt"));
89 String name;
90 int flag=0;
91 Scanner sc=new Scanner(System.in);
92 System.out.print("Enter an id of the student you want to search: " );
93 String searchname=sc.next();
94
95 while((name = file.readLine()) != null)
96 {
97 String[] line = name.split(" ");
98
99 if(searchname.equalsIgnoreCase(line[1]))
100 {
101 System.out.println("Record found");
102 System.out.println(name);
103 System.out.println("");
104 flag=1;
105 break;
106 }
107 }
108 if(flag==0)
109 System.out.println("Record not found");
110 file.close();
111 }
112
113 catch(FileNotFoundException e)
114 {
115 System.out.println("\nERROR : File not Found !!!");
116 }
117 }
118
119 //addRecords method
120
121 public void deleteRecords() throws IOException
122 {
123 try
124 {
125 BufferedReader file1 = new BufferedReader(new FileReader("sample.txt"));
126 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("new.txt",true)));
127 String name;
128 int flag=0;
Page 2 of 6
Main.java 01-11-2023 06:15
Page 3 of 6
Main.java 01-11-2023 06:15
193 flag=0;
194 }
195 else
196 {
197 System.out.println("Record found");
198 System.out.print("Enter updated marks: ");
199 String up_mark=sc.next();
200 pw.println(line[0]+" "+line[1]+" "+line[2]+" "+line[3]+" "+line[4]+" "+up_mark);
201 flag=1;
202 }
203 }
204
205 file1.close();
206 pw.close();
207 File delName = new File("sample.txt");
208 File oldName = new File("new.txt");
209 File newName = new File("sample.txt");
210
211 if(delName.delete())
212 System.out.println("record updated successfully");
213 else
214 System.out.println("Error");
215
216 if (oldName.renameTo(newName))
217 System.out.println("Renamed successfully");
218 else
219 System.out.println("Error");
220 }
221
222 catch(FileNotFoundException e)
223 {
224 System.out.println("\nERROR : File not Found !!!");
225 }
226 }
227
228 //addRecords method
229
230 public void clear(String filename) throws IOException
231 {
232 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
233 pw.close();
234 System.out.println("\nAll Records cleared successfully !");
235 }
236 }
237
238 // MAIN Class
239
240 public class Main
241 {
242 public static void main(String args[]) throws IOException {
243 Database f = new Database();
244 Scanner sc =new Scanner(System.in);
245 System.out.println("");
246 while(true)
247 {
248 System.out.print("1. Add Records\n2. Display Records\n3. Clear All Records\n4. Search Records"
249 + "\n5. Delete Records\n6. Update Records \n7. Exit\n\nEnter your choice : ");
250 int choice = sc.nextInt();
251 System.out.println("");
252
253 switch(choice)
254 {
255 case 1:
256 f.addRecords();
Page 4 of 6
Main.java 01-11-2023 06:15
257 System.out.println("\n====================================================\n");
258 break;
259
260 case 2:
261 f.readRecords();
262 System.out.println("\n====================================================\n");
263 break;
264
265 case 3:
266 f.clear("sample.txt");
267 System.out.println("\n====================================================\n");
268 break;
269
270 case 4:
271 f.searchRecords();
272 System.out.println("\n====================================================\n");
273 break;
274
275 case 5:
276 f.deleteRecords();
277 System.out.println("\n====================================================\n");
278 break;
279
280 case 6:
281 f.updateRecords();
282 System.out.println("\n====================================================\n");
283 break;
284
285 case 7:
286 System.out.println("\n====================================================\n");
287 System.exit(0);
288 break;
289
290 default:
291 System.out.println("\nInvalid Choice !");
292 System.out.println("\n====================================================\n");
293 break;
294 }
295 }
296
297 }
298
299 }
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
Page 5 of 6
Main.java 01-11-2023 06:15
321
322
323
324
325
326
327
328
329
330
331
332
333
334
Page 6 of 6