1. Write with PrintWriter
Java code
Import java. io .*;
Public class test {
/**
* @ Param args
*/
Public static void main (String [] args ){
String path = "e: // a.txt ";
Try {
FileWriter fw = new FileWriter (path, true );
PrintWriter pw = new PrintWriter (fw );
Pw. println ("world ");
Pw. close ();
// Bw. close ();
Fw. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
2. Write Data Using BufferedWriter
Java code
Import java. io .*;
Public class test {
/*** @ Param args */
Public static void main (String [] args ){
String path = "e: // a.txt ";
Try {
FileWriter fw = new FileWriter (path, true );
BufferedWriter bw = new BufferedWriter (fw );
Bw. newLine ();
Bw. write ("Hello ");
Bw. newLine ();
Bw. write ("world ");
Bw. close ();
Fw. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
// Used to read files
// BufferedReader br = new BufferedReader (new FileReader (new File ("e: // a.txt ")));
// Br. readLine (); // read a row
Java code
Java code
Package test;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. FileWriter;
Import java. io. IOException;
Import java. io. PrintWriter;
Import java. io. RandomAccessFile;
Public class FileRW {
Public static void main (String [] ){
Try {
FileOutputStream fos = new FileOutputStream (new File ("d: \ abc.txt"), true );
String str = "ABC \ n ";
Fos. write (str. getBytes ());
Fos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
Try {
FileWriter fw = new FileWriter ("d: \ abc.txt", true );
PrintWriter pw = new PrintWriter (fw );
Pw. println ("append content ");
Pw. close ();
Fw. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
Try {
RandomAccessFile rf = new RandomAccessFile ("d: \ abc.txt", "rw ");
Rf. seek (rf. length (); // move the pointer to the end of the file
Rf. writeBytes ("Append a line again! \ N ");
Rf. close (); // close the file stream
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
Author: "sd4886656"