1

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());
Robin Green
  • 32,079
  • 16
  • 104
  • 187
masmic
  • 3,526
  • 11
  • 52
  • 105
  • 1
    Don't tell me you've googled "java write at the end of a file", I wouldn't believe you. – Julien Nov 28 '13 at 09:22
  • http://stackoverflow.com/questions/8544771/how-to-write-data-with-fileoutputstream-without-losing-old-data – TheStackHasGrownIntoTheHeap Nov 28 '13 at 09:22
  • I've tryed writting at the end of the file, but then doesn't read right the string. I'm starting with java, sorry if for you is an easy thing. I don't understand why down vote if this supposses to be a place where people asks his doubts and learns – masmic Nov 28 '13 at 09:25

1 Answers1

4

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();
}
AJ.
  • 4,526
  • 5
  • 29
  • 41
  • I have tryed with that, but doesn't read the new string I've written, reads the string that was there before. Do I have to do the same with the `new FileInputStream(file, true)`? – masmic Nov 28 '13 at 09:22
  • @masmic_87 No, you cant do that. – AJ. Nov 28 '13 at 09:28
  • Ok, even if I put `true` there, it doesn't write/read correctly, if I write one string, reads the one I had before, n othe new one. If I write the second one, the throws this: `Error reading the file invalid type code: AC` – masmic Nov 28 '13 at 09:30
  • could you edit your post adding this modification in my code? I would be very appreciated if you could do this, because I'm blocked on this point. – masmic Nov 28 '13 at 09:35
  • As I told you before, can you modify my code and add it to your answer with the edit you're sugesting? I'm newbie in java and even I understand what you mean, I'm not sure how to do it – masmic Nov 28 '13 at 09:42
  • @masmic_87 Use the DataOutputStream. Se the edit in answer – AJ. Nov 28 '13 at 09:43
  • Ok, I'll try with that, let's see if it works now. – masmic Nov 28 '13 at 09:44
  • One doubt I've got, in the `new DataOutputStream(f)` does the f represent something, or do i have to put there `fileOutPutStream` – masmic Nov 28 '13 at 10:06
  • @masmic_87 yes f stands for fileOutputStream – AJ. Nov 28 '13 at 10:08