Filewriter character output stream and filereader character output stream

Source: Internet
Author: User

// 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 ();
}
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.