Question

I need to write strings on an file on eclipse without overwriting old strings. The function would be something like: create a string, save it on the file, create another string, save it on the file, this with several strings.

The strings have the next format:

String one = name surname surname; value1 value2 value3; code

So the method will be: create string, save it on the file. Create another string, save it on the file, etc.. Then after saving the wanted amount of strings on the file, I would need to read the file listing all the strings on the console.

But for now, I only get to save one string on the file and then list it. If I save two strings, the second overwrites the first, and anyway, it doesn't it do right because returns me null value when I want to list them.

This is the method which writes string on file:

public void writeSelling(List<String> wordList) throws IOException {
    fileOutPutStream = new FileOutputStream (file);
    write= new ObjectOutputStream (fileOutPutStream);
    for (String s : wordList){
        write.writeObject(s);
    }
    write.close();
}

This is how I call write method on the main class:

    List<String> objectlist= new ArrayList<String>();
    objectlist.add(product); //Product is the string I save each time 
                             //which has the format I commented above
    writeSelling(objectlist);

This is the method which reads strings from file:

public ArrayList<Object> readSelling() throws Exception, FileNotFoundException, IOException {
    ArrayList<Object> objectlist= new ArrayList<Object>();
    fileInPutStream = new FileInputStream (file);
    read= new ObjectInputStream (fileInPutStream);
    for (int i=0; i<contador; i++){
        objectlist.add(read.readObject());
    }
    read.close();
    return objectlist;
}

And this is how I call read on the main class:

ArrayList sellingobjects;
sellingobjects= readSelling();
for (Iterator it = sellingobjects.iterator(); it.hasNext();) {
        String s = (String)it.next();
}
System.out.println(s.toString());
Was it helpful?

Solution

You should open file like this for appending string in the file

new FileOutputStream(file, true)

Creates a file output stream to write to the file represented by the specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.

But Java serialization does not support "appending". you can't write an ObjectOutputStream to a file, then open the file again in append mode and write another ObjectOutputStream to it. you have to re-write the entire file every time. (i.e. if you want to add objects to the file, you need to read all the existing objects, then write the file again with all the old objects and then the new objects).

I would sugest you to use DataOutputStream

public void writeSelling(List<String> wordList) throws IOException {
    fileOutPutStream = new FileOutputStream (file,true);
    DataOutputStream write =new DataOutputStream(fileOutPutStream);
    for (String s : wordList){
        d.writeUTF(s);
    }
    write.close();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top