Package org.outman.dms.server;
Import Java.io.BufferedWriter;
Import Java.io.FileOutputStream;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import Java.io.RandomAccessFile; /** * Description: Append content to end of file * @author Administrator */public class Writestreamappend {/
* * Append files: Using FileOutputStream, when constructing FileOutputStream, set the second argument to True * * @param fileName * @param content */public static void Method1 (string file, String conent) {Bufferedwr
ITER out = null; try {out = new BufferedWriter (New OutputStreamWriter FileOutputStream (fil
E, true));
Out.write (conent);
catch (Exception e) {e.printstacktrace (); Finally {try {out.close ();
catch (IOException e) {e.printstacktrace ();
}}/** * Append files: Using filewriter * * @param fileName
* @param content */public static void Method2 (string fileName, string content) { try {//Open a write file, the second argument in the constructor is true to write the file in append form FileWriter writer = new FileWriter (Filenam
E, true);
Writer.write (content);
Writer.close ();
catch (IOException e) {e.printstacktrace (); }/** * Append files: Using randomaccessfile * * @param fileName * FileName * @param content * Append/public static void Method3 (S)
Tring FileName, String content) {try {//Open a random Access file stream, read-write Randomaccessfile randomfile = new Randomaccessfile (fileName, "RW");
File length, byte number long filelength = Randomfile.length ();
Moves the write file pointer to the end of the file.
Randomfile.seek (filelength);
String S2=new string (content.getbytes ("GBK"), "iso8859-1");
Randomfile.writebytes (S2);
Randomfile.close ();
catch (IOException e) {e.printstacktrace ();
} public static void Main (string[] args) {System.out.println ("start");
Method1 ("C:/work/test.txt", "Append to end of file 1");
METHOD2 ("C:/work/test.txt", "Append to end of File 2");
Method3 ("C:/work/test.txt", "Append to end of File 3");
System.out.println ("End"); }
}