import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;
class Record implements Serializable
{
int roll;
String name;
public void setData()
{
Scanner scr=new Scanner(System.in);
System.out.println("Enter Roll Number: ");
roll=scr.nextInt();
System.out.println("Enter Name: ");
name=scr.next();
writeData();
}
public void writeData()
{
try {
ObjectOutputStream of=new ObjectOutputStream(new FileOutputStream("c:\\yyy.txt",true));
of.flush();
Object o =new Object();
o=(Object)this;
of.writeObject(o);
//of.reset();
of.flush();
of.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void readData()
{
Record r;
try {
ObjectInputStream iff=new ObjectInputStream(new FileInputStream("c:\\yyy.txt"));
while(true)
{
r=(Record)iff.readObject();
r.getData();
}
}catch(EOFException e)
{
System.err.print("hi");
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getData()
{
System.out.println("Roll Number: " +roll);
System.out.println("Name : "+name);
}
}
public class java implements Serializable{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int ch;
Record obj=new Record();
while(true)
{
System.out.println("1::::::::WriteData");
System.out.println("2::::::::readData");
System.out.println("3::::::::Exit");
System.out.println("Enter Choice ");
ch=scr.nextInt();
switch(ch)
{
case 1:
obj.setData();
break;
case 2:
try {
Object o;
ObjectInputStream iff=new ObjectInputStream(new FileInputStream("c:\\yyy.txt"));
while(true)
{
o=iff.readObject();
obj=(Record)o;
obj.getData();
}
}catch(EOFException e)
{
System.err.print("hi");
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
case 3:
System.exit(0);
}
}
}
}
MONTS 0 Newbie Poster
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
Seldar 9 Newbie Poster
MONTS 0 Newbie Poster
Seldar 9 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.