1. Reading and Writing from one file to another
Public static void main (string [] ARGs )...{
Try ...{
File rfile = new file ("E:/test/text1.txt ");
File wfile = new file ("E:/test/text2.txt ");
Bufferedreader BR = new bufferedreader (New filereader (rfile ));
Bufferedwriter BW = new bufferedwriter (New filewriter (wfile, true);/* If this parameter is set to false, the original content of text2.txt is overwritten */
String tempstr = Br. Readline ();
While (tempstr! = NULL)
...{
// BW. Write (tempstr );
Bw. append (tempstr );
Bw. newline ();
Bw. Flush ();
Tempstr = Br. Readline ();
}
Bw. Close (); // close this first
BR. Close ();
} Catch (exception e )...{
System. Out. println (E. getmessage ());
}
}
2. Get the characters entered from the keyboard and write them to the file
Try ...{
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));
System. Out. Print ("enter a line :");
String strin = stdin. Readline ();
System. Out. println (strin );
Bufferedwriter BW = new bufferedwriter (New filewriter (new file ("E:/test/text2.txt ")));
Bw. Write (strin );
Bw. Flush ();
Stdin. Close ();
Bw. Close ();
} Catch (exception e )...{
System. Err. println (E. getmessage ());
}