// Filewriter
Public class filewriterdemo {
// Delimiter stream: Applicable to text files, which are operated in characters and often used together with buffered streams.
/**
* Operation steps of the livestream:
* 1. Create a producer stream
* 2. Create a buffer stream
* 3. Perform read/write operations
* 4. Close the stream
*/
// Note when using a buffer stream: to forcibly write data in the buffer stream to a file, 1) Use the flush method 2) disable the stream.
// Producer stream (write operation)
Public static void main (string [] ARGs ){
Filewriter fw = NULL;
Bufferedwriter BW = NULL;
Try {
// 1. Create a producer stream
FW = new filewriter ("abc.txt ");
// 2. Create a buffer stream
BW = new bufferedwriter (FW );
// 3. write operation
Bw. Write ("ABC ");
Bw. Write ("\ r \ n"); // line feed (method 1)
Bw. newline (); // line feed (method 2)
Bw. Write ("www ");
// BW. Flush (); // clear the buffer stream
System. Out. println ("Write successful! ");
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Finally {
Try {
// 4. Close the stream
Bw. Close ();
FW. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
}
// Filereader
Public class filereaderdemo {
// Upstream stream (read operation)
Public static void main (string [] ARGs ){
Filereader Fr = NULL;
Bufferedreader BR = NULL;
Try {
Fr = new filereader ("abc.txt ");
BR = new bufferedreader (FR );
String STR = NULL; // stores the character data read in each loop.
While (STR = Br. Readline ())! = NULL ){
System. Out. println (STR );
}
} Catch (filenotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Finally {
Try {
BR. Close ();
Fr. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
}