使用outputstream将数据添加到特定文件

时间:2012-02-23 05:21:42

标签: java io fileoutputstream

String data="this we want append in that file";
byte[] getbyte = data.getBytes();
outputstream.write(getbyte);
outputstream.flush();

通过使用这些代码我试图在特定文件中写入数据,但数据不会被添加到该文件

1 个答案:

答案 0 :(得分:0)

import java.io。*;

public class WriteFileExample {

public static void main(String [] args){

String strFilePath = "d:/FileIO.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);
 }

}   }