Append string to a text file : PrintStream « File Input Output « Java






Append string to a text file

  

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Main {
  public static void main(String[] argv) {
    append(new File("c:\\a.txt"), "value");
  }

  public static void append(File aFile, String content) {
    try {
      PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(aFile, true)));
      p.println(content);
      p.close();

    } catch (Exception e) {
      e.printStackTrace();
      System.err.println(aFile);
    }
  }

}

   
    
  








Related examples in the same category

1.Create print stream for error logger
2.Save string value to a file