Append string to a text file with buffer in Java

Description

The following code shows how to append string to a text file with buffer.

Example


//  w ww.ja va 2 s  .  c om
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);
    }
  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip