M
Madhur Ahuja
Hello
I have been given the task of doing simple file handling in java.
i.e. I want to write set of records to a file and read them.
The problem is that writeObject function produces inconsistent
results when we write a series of records in one session and when
we write same number of records by running the program again and again
i.e. appending.
The file size differs in above 2 cases.
Thus reading the records produces this exception:
StreamCorruptedException
This exception occurs only when we read from the file in which data
has been appended.
The exception doesnt occurs if we write to file only once and then read.
The code that am using is:
////////////////////////////
import java.io.*;
public class serial6
{
FileOutputStream fos = null;
FileInputStream fis = null;
ObjectOutputStream oos = null;
public serial6()
{
try
{
MyClass1 obj1 = new MyClass1("sasas",12);
MyClass1 obj2 = new MyClass1("d",12);
try
fis = new FileInputStream("serial133.txt");
fis.close();
fos = new FileOutputStream("serial133.txt",true);
}
catch(FileNotFoundException e)
{
fos = new FileOutputStream("serial133.txt");
}
oos = new ObjectOutputStream(fos);
oos.writeObject(obj1);
oos.writeObject(obj2);
}
catch( IOException e)
{
}
try
{
oos.close();
} catch(IOException e) {
}
}
public serial6(int i)
{
try
{
FileOutputStream fos1 = new FileOutputStream("serial33.txt",true);
} catch(IOException e) {
}
}
public static void main(String args[])
serial6 sr = new serial6(1);
serial6 sr1 = new serial6();
}
}
class MyClass1 extends Object implements Serializable
{
String name;
int roll;
MyClass1(String a,int b)
{
this.name=a;
this.roll=b;
}
}
--
Winners dont do different things, they do things differently.
Madhur Ahuja
India
Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
I have been given the task of doing simple file handling in java.
i.e. I want to write set of records to a file and read them.
The problem is that writeObject function produces inconsistent
results when we write a series of records in one session and when
we write same number of records by running the program again and again
i.e. appending.
The file size differs in above 2 cases.
Thus reading the records produces this exception:
StreamCorruptedException
This exception occurs only when we read from the file in which data
has been appended.
The exception doesnt occurs if we write to file only once and then read.
The code that am using is:
////////////////////////////
import java.io.*;
public class serial6
{
FileOutputStream fos = null;
FileInputStream fis = null;
ObjectOutputStream oos = null;
public serial6()
{
try
{
MyClass1 obj1 = new MyClass1("sasas",12);
MyClass1 obj2 = new MyClass1("d",12);
try
fis = new FileInputStream("serial133.txt");
fis.close();
fos = new FileOutputStream("serial133.txt",true);
}
catch(FileNotFoundException e)
{
fos = new FileOutputStream("serial133.txt");
}
oos = new ObjectOutputStream(fos);
oos.writeObject(obj1);
oos.writeObject(obj2);
}
catch( IOException e)
{
}
try
{
oos.close();
} catch(IOException e) {
}
}
public serial6(int i)
{
try
{
FileOutputStream fos1 = new FileOutputStream("serial33.txt",true);
} catch(IOException e) {
}
}
public static void main(String args[])
serial6 sr = new serial6(1);
serial6 sr1 = new serial6();
}
}
class MyClass1 extends Object implements Serializable
{
String name;
int roll;
MyClass1(String a,int b)
{
this.name=a;
this.roll=b;
}
}
--
Winners dont do different things, they do things differently.
Madhur Ahuja
India
Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com