Java I/O operations are performed based on streams. To improve read/write efficiency, buffer is required.
Simple ExampleProgramAs follows:
/**
* Read the content in 1.txt and write it to 2.txt.
*
*/
Import java. Io .*;
Public class readwritefile {
Public static void main (string [] ARGs ){
Try {
File Read = new file ("C: // 1.txt ");
File write = new file ("C: // 2.txt ");
Bufferedreader BR = new bufferedreader (
New filereader (read ));
Bufferedwriter BW = new bufferedwriter (
New filewriter (write ));
String temp = NULL;
Temp = Br. Readline ();
While (temp! = NULL ){
// Write an object
Bw. Write (temp + "/R/N"); // applicable only to Windows
// Continue reading the file
Temp = Br. Readline ();
}
Bw. Close ();
BR. Close ();
} Catch (filenotfoundexception e) {// file not found
System. Out. println (E );
} Catch (ioexception e ){
System. Out. println (E );
}
}
}