views:

500

answers:

3

This code appends to an already created Excel file:

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");

What can we add / modify so that Decrypted.xls should be created if not already created and appended if already created?

A: 

According to the Javadocs for the String-accepting constructor of FileOutputStream, rover12, if the file doesn't already exist then it's created. Are you not seeing this behavior?

(And as others have mentioned, be sure to use the constructor that takes the second boolean argument so you can specify that you want to append the file if it already exists...)

delfuego
no i am not seeing this behaviour .. if i delete the already existing Decrypted.xls i get an error compliling that says .. file does not exist
rover12
no you're right .. it does get created.. sorry
rover12
+1  A: 

You want the FileOutputStream(File file, boolean append) constructor for switching on whether you truncate or append.

uckelman
A: 

Use the constructor:

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls", true);

to append to an existing file, if it doesn't exist. Your example will overwrite the existing one.

Andreas_D