-
Notifications
You must be signed in to change notification settings - Fork 0
/
P7.java
114 lines (103 loc) · 2.45 KB
/
P7.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
package rank;
import java.io.*;
import java.util.*;
public class P7 implements Serializable
{
int eid;
String name;
transient float sal;
static Scanner sc= new Scanner(System.in);
public P7()
{}
public P7(int e,String n, float s)
{
eid=e;
name=n;
sal=s;
}
static public P7 add()
{
P7 p;
int e;
String n;
float s;
System.out.println("Enter details: ");
System.out.println("Name: ");
n= sc.next();
System.out.println("Employee ID: ");
e=sc.nextInt();
System.out.println("Salary(in lakhs): ");
s=sc.nextFloat();
p=new P7(e,n,s);
return p;
}
static public void write(P7 p)
{
try
{
FileOutputStream file = new FileOutputStream("C:/Users/HP/Desktop/State.txt",true);
AppendingObjectOutputStream out = new AppendingObjectOutputStream(file);
out.writeObject(p);
out.close();
file.close();
out.flush();
file.flush();
System.out.println("Serialized!");
}
catch(IOException ex)
{
System.out.println("EXCEPTION!!"+ex);
}
}
static public void read()
{
int i=0;
P7 p;
try
{
FileInputStream file = new FileInputStream("C:/Users/HP/Desktop/State.txt");
AppendingObjectInputStream in = new AppendingObjectInputStream(file);
while(file.available()>0)
{
p = (P7)in.readObject();
i=+1;
System.out.println("Deserialised Object "+i);
System.out.println("EID: " + p.eid);
System.out.println("Name: " + p.name);
System.out.println("Salary: " + p.sal);
}
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException"+ex);
}
}
public static void main(String args[])
{
while(true)
{
System.out.println("0. Exit");
System.out.println("1. Write");
System.out.println("2. Read");
System.out.println("Enter your choice: ");
int c=sc.nextInt();
switch(c)
{
case 0:
System.exit(0);
case 1:
P7 p= add();
write(p);
break;
case 2:
read();
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}