This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hai i need Help in FileOutputStream !  RSS feed

 
Matt Swaggi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai friends ! When i try to compile the below code , the data is being overridden , i need append the lines ,i mean i need to add few more line without replacing the old lines.
can you help me in that ? The code is as follows

CODE

import java.io.*;

public class WriteByteArrayToFile {

public static void main(String[] args) {

String strFilePath = "f://File//demo.txt";

try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
String strContent = "Write File using Java FileOutputStream example !";

/*
* To write byte array to a file, use
* void write(byte[] bArray) method of Java FileOutputStream class.
*
* This method writes given byte array to a file.
*/

fos.write(strContent.getBytes());

/*
* Close FileOutputStream using,
* void close() method of Java FileOutputStream class.
*
*/

fos.close();

}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException : " + ex);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}

}
}
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i get it correctly, you want to append data to existing file.
If yes, then use this constructor of FileOutputStream with the value of append as true.
[ August 05, 2008: Message edited by: Nitesh Kant ]
 
Matt Swaggi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you alter the code and point it to me very clearly friend.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileOutputStream fos = new FileOutputStream(strFilePath, true);

By the way, have you read the JavaDoc as Nitesh pointed out?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guna Ranjan:
can you alter the code and point it to me very clearly friend.


You will be more clear if you read the javadocs and try to do it yourself!
 
Matt Swaggi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you bye
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!