Jovan Dukic
Jovan Dukic

Reputation: 83

How to add an object to file every time i run my program in java

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();
}

}

Upvotes: 2

Views: 190

Answers (2)

M S Praful
M S Praful

Reputation: 51

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"));

Upvotes: 1

JDevMAN
JDevMAN

Reputation: 1

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)

Upvotes: 0

Related Questions