- Joined
- Dec 30, 2009
- Messages
- 3
- Reaction score
- 0
I have used the following code to write into a file using object stream.
public void writeData() {
try {
oos = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(new File("D:/xyz.dat"), true)));
DataContainer oContainer= new DataContainer(i++, i);
oos.writeObject(oContainer);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public class DataContainer implements Serializable{
private int id;
private int data;
public DataContainer(int id,int data){
this.id=id;
this.data=data;
}
public void printData(){
System.out.println("ID->>"+this.id+"\tDATA->>"+this.data);
}
}
And I try to read with the following code
public void readaData() {
try {
ObjectInputStream ois = new ObjectInputStream(new GZIPInputStream(new FileInputStream("D:/xyz.dat")));
while (true) {
try {
DataContainer oDataContainer = (DataContainer) ois.readObject();
oDataContainer.printData();
} catch (Exception e1) {
e1.printStackTrace();
break;
}
}
ois.close();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
While on read i'm getting EOF File exception before the expected read operation.
I could observe that Once if I use ois.close(); in the write() method then if again I am appending to that file then after the first write() an EOF is placed in the file which blocks me from the requirement.
My requirement is that I want to append Object data to a file in compressed format and read it fully
Can anyone can help me in this regard.
public void writeData() {
try {
oos = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(new File("D:/xyz.dat"), true)));
DataContainer oContainer= new DataContainer(i++, i);
oos.writeObject(oContainer);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public class DataContainer implements Serializable{
private int id;
private int data;
public DataContainer(int id,int data){
this.id=id;
this.data=data;
}
public void printData(){
System.out.println("ID->>"+this.id+"\tDATA->>"+this.data);
}
}
And I try to read with the following code
public void readaData() {
try {
ObjectInputStream ois = new ObjectInputStream(new GZIPInputStream(new FileInputStream("D:/xyz.dat")));
while (true) {
try {
DataContainer oDataContainer = (DataContainer) ois.readObject();
oDataContainer.printData();
} catch (Exception e1) {
e1.printStackTrace();
break;
}
}
ois.close();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
While on read i'm getting EOF File exception before the expected read operation.
I could observe that Once if I use ois.close(); in the write() method then if again I am appending to that file then after the first write() an EOF is placed in the file which blocks me from the requirement.
My requirement is that I want to append Object data to a file in compressed format and read it fully
Can anyone can help me in this regard.