views:

752

answers:

2

I have files in who i need to record serialized object. I open ObjectOutputStream for writing in files. If i didnt wrote nothing in file, file content get deleted. I don't want content to be deleted when i make ObjectOutputStream. Any help?

Edit:

@Provides
@ArticleSerializationOutputStream
public ObjectOutputStream getArticleObjectOutputStream(Config config) {
  ObjectOutputStream out = null;
  String fileName = config.getConfigValue(ARTICLE_SNAPSHOT);
  try {
    out = new ObjectOutputStream(new FileOutputStream(new File(fileName)));
  } catch (IOException e) {
    String errorMessage = String.format(IO_EXCEPTION_PROBLEM, fileName);
    addError(errorMessage);
  }
  return out;
}

this is the code.... I use Guice!

+6  A: 

Creating the ObjectOutputStream itself won't overwrite anything. I suspect you just created a new FileOutputStream which will have truncated any current content unless you tell it to append. I think you want:

FileOutputStream fos = new FileOutputStream(filename, true);

to make it append to a file instead of overwriting.

EDIT: Yes, as per your edit, you're creating a new FileOutputStream without telling it to append. It's therefore overwriting the file.

Jon Skeet
Why is reading the bloody documentation so hard? I don’t get it. :(
Bombe
A: 

I solve the problem. I use provider instead instancing the OutputStrams in begin with Guice.

@Inject @ArticleOutputStream Provider articleObjectOutputStream;

articleOutputStream.get();

Work fine for now.