2

I Have 3 objects of Person type and i write them to my file Person.person and than i read them one by one but when i run my program again i get the same 3 objets written as when i run it for the firts time.How to add those 3 objects to my file again and again every time i run my program.I want from it to be the same 3 objects or some other objects but they need to be added to the end of my file.

And how to get that data when i want to read that file?

 package pisanjeUFajl;

 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.EOFException;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.List;

 public class Serilizacija {
 public static void main(String[] args) throws IOException, 
 ClassNotFoundException {

    Person person = new Person("Jovan", "Dukic");
    Person person2 = new Person("Stanko", "Maca");
    Person person3 = new Person("Tole", "Lopove");

    File file = new File("C:\\Users\\Jovan\\Desktop\\Person.person");
    if (!(file.exists())) {
        file.createNewFile();
    }

    ObjectOutputStream outputStream = null;

    outputStream = new ObjectOutputStream(new BufferedOutputStream(new 
    FileOutputStream(file)));

    outputStream.writeObject(person);
    outputStream.reset();
    outputStream.writeObject(person2);
    outputStream.reset();
    outputStream.writeObject(person3);
    outputStream.reset();

    outputStream.close();

    ObjectInputStream inputStream = null;

    inputStream = new ObjectInputStream(new BufferedInputStream(new 
    FileInputStream(file)));
    int count = 0;
    Person object = null;

I added more objects to my file but when i wanted to read them i got and error: Exception in thread "main" java.io.StreamCorruptedException: invalid typecode:AC

at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at pisanjeUFajl.Serilizacija.main(Serilizacija.java:46)

It read just the first 3 objects which were added the first to my file.

    try {
        while (true) {
            object = (Person) inputStream.readObject();
            count++;
            System.out.println(object);

        }
    } catch (EOFException error) {
        System.out.println("\nEnd of file reacher - objects read = " + 
    count);
    }

    inputStream.close();
}

}

2 Answers2

1
Use for writing object       
ObjectOutputStream  outputStream = new ObjectOutputStream(new 
                        FileOutputStream(new File("test")));



Use it for appending object at end of file 
ObjectOutputStream outputStream2 = new ObjectOutputStream(new FileOutputStream("test", true)) {
                    protected void writeStreamHeader() throws IOException {
                        reset();
                    }
                };

for reading ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("test"));

M S Praful
  • 51
  • 2
  • But how to use outputStream2 in my code i mean i dont know how to call it on my objects because you said i need to use outputStream.writeObject() to write object than when i need to use outputStream2.writeObject()? – Jovan Dukic Nov 16 '17 at 16:07
  • outputStream2 .writeObject(new TestSomeObject()); outputStream2 .close(); – M S Praful Nov 16 '17 at 16:23
0

Use the Constructor of FileOutputStream that accepts a boolean as a parameter. If its set to true, everything written to file through this Stream is appended to the existing content. Constructor looks like this:

    FileOutputStream(File file, boolean append)
JDevMAN
  • 1
  • 1
  • i got an error when i want to read my file : Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at pisanjeUFajl.Serilizacija.main(Serilizacija.java:46) – Jovan Dukic Nov 16 '17 at 15:39
  • Sorry for answering that late. Of course I don't know your full code but i think this answer might help you: https://stackoverflow.com/a/2395269/8944371 – JDevMAN Dec 14 '17 at 14:59