How ava additional writing txt file

(A) Overview of three methods br /> in java, the contents of the file append operation:
Import java.io.BufferedWriter;
Import java.io.FileOutputStream;
Import java.io.FileWriter;
Import java.io.IOException;
java.io.OutputStreamWriter Import;
Import java.io.PrintWriter;
Import java.io.RandomAccessFile;
// If the file exists, then additional content; if the file does not exist, create a file, the additional content of the three methods
public class AppendContentToFile {
@SuppressWarnings ( "static-Access")
public static void main (String [] args) {
AppendContentToFile new new AppendContentToFile A = ();
a.method1 ();
a.method2 ( "E: \ it as dd.txt", "222,222,222,222,222" );
a.method3 ( "E: \ dd.txt", "33,333,333,333");

(B) Method 1
public void method1 () {
FileWriter fw = null;
the try {
// If the file exists, the additional content; if the file does not exist, create file
File f = new File ( "E : \ dd.txt" );
FW = new new FileWriter (F, to true);
} the catch (IOException E) {
e.printStackTrace ();
}
the PrintWriter the PrintWriter new new PW = (FW);
pw.println ( "additional content");
pw.flush () ;
the try {
fw.flush ();
pw.close ();
fw.close ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
(c) method 2
public static void method2 (File String, String conent) {
BufferedWriter, OUT = null;
the try {
OUT = new new BufferedWriter, (the OutputStreamWriter new new (
a FileOutputStream new new (File, to true)));
out.write (conent + "\ R & lt \ n-");
} the catch (Exception E) {
e.printStackTrace ();
} {the finally
the try {
the out.close ();
} the catch ( E IOException) {
e.printStackTrace ();
}
}
}
(iv) method. 3
public static void the method3 (fileName String, String Content) {
the try {
// open a random access file stream, both reading and writing
RandomAccessFile randomFile = new RandomAccessFile (fileName, "RW");
// file length, the number of bytes
Long fileLength randomFile.length = ();
// write the file pointer to the end of the file.
randomFile.seek (fileLength);
randomFile.writeBytes (Content "\ R & lt \ n-" +);
randomFile.close ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
}

Guess you like

Origin blog.51cto.com/403573/2425853