You are on page 1of 6

Main.

java 01-11-2023 06:15

1 // Prachi Tupe SEIT B S512058


2
3 //Problem Statement::
4 /*
5 Implement a program for maintaining a student records database using File Handling.
6 Student has Student_id, name, Roll_no, Class, marks and address. Display the data
7 for five students.
8 */
9
10 import java.io.*;
11 import java.util.*;
12
13 // FILE CLASS
14
15 class Database
16 {
17 static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18 public void addRecords() throws IOException
19 {
20 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("sample.txt",true)));
21 String studentname, address,s;
22 int studentid, rollno;
23 float marks;
24
25 boolean addMore = false;
26 do
27 {
28 System.out.print("\nEnter Student Name: ");
29 studentname = br.readLine();
30 System.out.print("Student Id: ");
31 studentid = Integer.parseInt(br.readLine());
32 System.out.print("Roll no: ");
33 rollno = Integer.parseInt(br.readLine());
34 System.out.print("Address: ");
35 address = br.readLine();
36 System.out.print("Class: ");
37 Class = Integer.parseInt(br.readLine());
38 System.out.print("Marks : ");
39 marks = Float.parseFloat(br.readLine());
40
41 pw.println(studentname+" "+studentid+" "+rollno+" "+address+" "+Class+" "+marks);
42 System.out.print("\nRecords added successfully !\n\nDo you want to add more records ? (y/n) : "
43 s = br.readLine();
44
45 if(s.equalsIgnoreCase("y"))
46 {
47 addMore = true;
48 System.out.println();
49 }
50 else
51 addMore = false;
52 }
53 while(addMore);
54 pw.close();
55 }
56
57 // addRecords method
58
59 public void readRecords() throws IOException
60 {
61 try
62 {
63 BufferedReader file = new BufferedReader(new FileReader("sample.txt"));
64 String name;

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

129 Scanner sc=new Scanner(System.in);


130 System.out.print("Enter the name of the student you want to delete: ");
131 String searchname=sc.next();
132
133 while((name = file1.readLine()) != null)
134 {
135 String[] line = name.split(" ");
136
137 if(!searchname.equalsIgnoreCase(line[0]))
138 {
139 pw.println(name);
140 flag=0;
141 }
142 else
143 {
144 System.out.println("Record found");
145 flag=1;
146 }
147 }
148
149 file1.close();
150 pw.close();
151
152 File delName = new File("sample.txt");
153 File oldName = new File("new.txt");
154 File newName = new File("sample.txt");
155
156 if(delName.delete())
157 System.out.println("deleted successfully");
158 else
159 System.out.println("Error");
160
161 if (oldName.renameTo(newName))
162 System.out.println("Renamed successfully");
163 else
164 System.out.println("Error");
165 }
166
167 catch(FileNotFoundException e)
168 {
169 System.out.println("\nERROR : File not Found !!!");
170 }
171 }
172
173 //addRecords method
174
175 public void updateRecords() throws IOException
176 {
177 try{
178 BufferedReader file1 = new BufferedReader(new FileReader("sample.txt"));
179 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("new.txt",true)));
180 String name;
181 int flag=0;
182 Scanner sc=new Scanner(System.in);
183 System.out.print("Enter the name of the student you want to update: ");
184 String searchname=sc.next();
185
186 while((name = file1.readLine()) != null)
187 {
188 String[] line = name.split(" ");
189
190 if(!searchname.equalsIgnoreCase(line[0]))
191 {
192 pw.println(name);

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

You might also like