Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

File I/O question

  Asked By: Sebastion    Date: Nov 12    Category: Java    Views: 462
  

I have a Java file question. I have two Java file. File1.java
and File2.java. Both files are identical but in different location.
In both files, I am write the string into a data.html file using the
following code.

File1.java
String FileName = File.separatorChar + "Temp" + File.separatorChar
+ "data.html" ;
fos = new FileOutputStream(FileName );
oos = new DataOutputStream(fos);
oos.writeBytes("File1" + "\n");

File2.java
String FileName = File.separatorChar + "Temp" + File.separatorChar
+ "data.html" ;
fos = new FileOutputStream(FileName );
oos = new DataOutputStream(fos);
oos.writeBytes("File2" + "\n");

Now, the problem I am having is the following

I run the File1.java and "File1" string get write into a data.html
file.
Now, I run the File2.java and "File2" string get write into a
data.html file.

The problem is : when I run the File2.java file then It overrite
the "File1" string

Does anyone have any idea of how would I print both string "File1"
and "File2". Please let me know, Thank you for your help.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Maurice Hanson     Answered On: Nov 12

Use FileOutputStream(String filename, boolean append);

 
Answer #2    Answered By: Bellona Lopez     Answered On: Nov 12

Only thing which u needa do is just open the your "data.html" file  in append
mode in File2.java. Then it'll just append the String to the file.

just replace

fos = new FileOutputStream(FileName ); in File2.java with

fos = new FileOutputStream(FileName,true );
which sets append value to true which is false by default.

 
Didn't find what you were looking for? Find more on File I/O question Or get search suggestion and latest updates.




Tagged: