Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Appending file problem  RSS feed

 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I'm have a perplexing problem I hope you can help me with.

Here is my problem.


I read a line from a text file using BufferedReader:

dataFile = new BufferedReader(new FileReader(fileName));
String line = dataFile.readLine();

I write the line as an object to a file using FileOutputStream and ObjectOutputStream:

FileOutputStream fos = new FileOutputStream ("file");
ObjectOutputStream oos = new ObjectOutputStream ( fos );
oos.writeObject(line.trim());

No problem so far....

I then read from the file created before using FileInputStream and ObjectInputStream:

in = new FileInputStream(fileName);
is = new ObjectInputStream(in);
Object Line=((Object) is.readObject());

Works fine....

I then want to append to the same file using:
ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream (fileName,true));

I get a line from the keyboard:

String newline = standard.readLine();

and append using :

oos.writeObject(newline.trim());

This appends the info to the end of the file but the appended info cannot be read back! I'm not sure about how the file looks after the append. The new info is surrounded by the ff chars \254\355^E.

Any help will be much appreciated.

Regards
Dominique
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ObjectOutputStream (OOS) includes a header. You can't append one OOS onto another this way and get a single OOS data file; you get two separate data files catenated together.
 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense. Thanks Ernest. Iuppose the solution then is to append with no header?
 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, I 've had a look around and I can't see how to not write the header or ignore the header. Any suggestions?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q: Doctor, it hurts when I do this. What should I do?

A: Don't do that!

I suggest one of the following options:
  • Don't use ObjectOutputStream at all if what you're writing is simply text. Write with a Writer, and read with a Reader. (A PrintWriter and BufferedReader will have a println() and readLine() respectively.) This will be simpler, unless you really, really, really need to write a file with multiple Object types, not just text.
  • Don't try to append to a file using an ObjectOutputStream. Instead, read the entire existing file, and write all the contents to a new file using a single OOS. When you are done rewriting all the old contents, write any additional new objects using the same OOS.

  • If you've tried both these and are certain they won't work for you, you could also try these:
  • Use a different OOS for each and every Object you write to the file. You will have to close each OOS before writing a new one. This means each object will have its own header. To be honest I'm not sure if this one will work, but it might possibly be worth a try.
  • Create a custom RedirectableOutputStream class which is similar to FilteredOutputStream (maybe even subclassing it) but allows you to reset the inner stream to a different stream after creation. Then you could eliminate the OOS header by: flushing, redirecting output to some stream which you can ignore (like a ByteArrayOutputStream), opening a new ObjectOutputStream which writes to the RedirectableOutputStream, flushing again, resetting the RedirectableOutputStream to write to a new FileOutputStream in append mode, and then writing your object to the OOS (and thus appending to the file). Again, I'm not sure this will work - it assumes that the header in the original OOS has all the information which might be needed by the later OOS, and I don't know if that's the case. Plus there are many opportunities here to introduce subtle bugs. But it might be worth a try if all the other ideas are unsuitable.

  • Hope that helps...
     
    Dominique Ramoney
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Jim, I'll try some of your suggestions.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!