• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hai i need Help in FileOutputStream !

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}

}
}
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you alter the code and point it to me very clearly friend.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you bye
 
reply
    Bookmark Topic Watch Topic
  • New Topic