Program to Append to file using FileOutputStream in java file IO




For appending content in file, keep second parameter as true,
using new FileOutputStream("c:/myFile.txt",true) will append content to file





Program to Append to file using FileOutputStream in java file IO >


import java.io.FileOutputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class AppendToFileUsingFileOutputStream {
   public static void main(String... args) {
          FileOutputStream fos = null;
          try {
                 String fileName = "c:/myFile.txt" ;
                 fos = new FileOutputStream(fileName, true);
                 String str="This string will be APPENDED in file";
                 fos.write(str.getBytes()); //convert String into byte array to write in file
                
                 fos.flush(); //bytes in buffer are written in file
                
                 System.out.println("String str has been APPENDED successfully in "+fileName
                              + " using FileOutputStream");
                
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (fos != null){
                              fos.close(); //close FileOutputStream
                       }
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
String str has been APPENDED successfully in c:/myFile.txt using FileOutputStream
*/


RELATED LINKS>

Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)


Program to Move a file - in java file IO


Program to Find file exists or not - in java file IO


Program on How to Compare two file or Directory path - in java file IO



Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO


Traverse a Directory (all sub-directories and files) in 2 ways




eEdit
Must read for you :