Java file operations: read and write by row
File operations are the most common part in any language, and Java is no exception. This section mainly introduces the operations on reading files and writing files by row.
ImportJava. Io. bufferedreader;
ImportJava. Io. bufferedwriter;
ImportJava. Io. filenotfoundexception;
ImportJava. Io. filereader;
ImportJava. Io. filewriter;
ImportJava. Io. ioexception;
Public ClassJavafile {
Public Static VoidMain (string [] ARGs ){
Try{
// Read File Content from File
Stringbuffer sb =NewStringbuffer ("");
Filereader reader =NewFilereader ("C: // test.txt ");
Bufferedreader BR =NewBufferedreader (Reader );
String STR =Null;
While(STR = Br. Readline ())! =Null){
SB. append (STR + "/N ");
System.Out. Println (STR );
}
BR. Close ();
Reader. Close ();
// Write string to file
Filewriter writer =NewFilewriter ("C: // test2.txt ");
Bufferedwriter BW =NewBufferedwriter (writer );
Bw. Write (sb. tostring ());
Bw. Close ();
Writer. Close ();
}
Catch(Filenotfoundexception e ){
E. printstacktrace ();
}
Catch(Ioexception e ){
E. printstacktrace ();
}
}
}