-
Notifications
You must be signed in to change notification settings - Fork 0
/
student.java
128 lines (128 loc) · 3.55 KB
/
student.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.*;
import java.util.*;
public class student implements Serializable
{
String regno;
String Name;
String Branch;
String City;
public String input() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please Enter the regno:- ");
this.regno=br.readLine();
System.out.print("Please Enter Name :- ");
this.Name=br.readLine();
System.out.print("Please Enter Branch:- ");
this.Branch=br.readLine();
System.out.print("Please Enter City:- ");
this.City=br.readLine();
return this.regno;
}
public void display()
{
System.out.println("\nRecord Entered :- \n");
System.out.println("Register Number:- "+this.regno);
System.out.println("Name:- "+this.Name);
System.out.println("Branch:- "+this.Branch);
System.out.println("City:- "+this.City);
}
public void modify() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Register Number:- "+this.regno);
System.out.print("Please Enter Name :- ");
this.Name=br.readLine();
System.out.print("Please Enter Branch:- ");
this.Branch=br.readLine();
System.out.print("Please Enter City:- ");
this.City=br.readLine();
}
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch=1;
try
{
while(ch<=5)
{
System.out.println("\n\n\t\tFile Management Menu");
System.out.println("1. Add Data Record ");
System.out.println("2. Delete File ");
System.out.println("3. Display Record:- ");
System.out.println("4. Modify Data ");
System.out.println("5. Exit ");
System.out.print("Please Enter Your Choice:- ");
ch=Integer.parseInt(br.readLine());
if(ch==1)
{
student s=new student();
String r=s.input();
String fname=(String)r+".txt";
File f=new File(fname);
if(!f.exists())
{
f.createNewFile();
System.out.println("File Name:- "+fname+" created");
}
else
{
System.out.println("File Already Exists!!");
continue;
}
FileOutputStream fout=new FileOutputStream(fname);
ObjectOutputStream oos=new ObjectOutputStream(fout);
oos.writeObject(s);
oos.flush();
oos.close();
fout.close();
System.out.println("\nRecord Written in File ");
}
if(ch==3)
{
System.out.print("Please Enter the register number:- ");
String r=br.readLine();
String fname=r+".txt";
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(fname));
student o=(student)ois.readObject();
ois.close();
o.display();
}
if(ch==2)
{
System.out.print("Please Enter the register number:- ");
String r=br.readLine();
String fname=r+".txt";
File f=new File(fname);
f.delete();
System.out.println("File "+fname+" deleted !!");
}
if(ch==4)
{
System.out.print("Please Enter the register number:- ");
String r=br.readLine();
String fname=r+".txt";
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(fname));
student o=(student)ois.readObject();
ois.close();
o.modify();
FileOutputStream fout=new FileOutputStream(fname);
ObjectOutputStream oos=new ObjectOutputStream(fout);
oos.writeObject(o);
oos.flush();
oos.close();
fout.close();
System.out.println("\nRecord Modified");
}
if(ch==5)
{
System.exit(0);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}