Java I/o---Reader & Writer (character stream)

Source: Internet
Author: User

1.Reader & Writer

When we first saw the Reader and Writer classes, we might think that this was two classes to replace InputStream and Outputstreamt , but that is not the case.

Although some of the original "stream" libraries are no longer used (if they are used, you will receive compiler warning messages), InputStream and Outputstreamt can still provide valuable functionality in byte-oriented I/O. Reader and writer provide Unicode-compatible and character-oriented I/O functionality.

Sometimes we have to combine the classes from the class in the "byte" hierarchy with the classes in the "character" hierarchy (implementing Byte and character conversions). To achieve this, the adapter (adapter) class is used: Lnputstreamreader (subclass FileReader) can convert InputStream to reader (Byte to character) and O Utputstreamwriter(Subclass FileWriter) can convert a outputstream into a byte.

Java I/O---character and byte conversion stream---Filereader&filewriter

2. BufferedReader & BufferedWriter

Two classes implement a character stream buffer

1  Public classCharstreambufferdemo {2 3 /** 4 * @param args 5 * @throws IOException 6 * *7  Public Static voidMain (string[] args)throwsIOException {8 9 *  /* The buffer that demonstrates the character stream. * BufferedReader *  bufferedwriter    * - //writetextbybuffered (); theReadtextbuffered (); -  - //Read Keyboard entry professional. Scanner= stream + Regular expression. method is to read the data according to a certain rule.  - //bufferedreader bufr = new BufferedReader (new InputStreamReader (system.in)) + //bufr.readline (); -} +  A  Public Static voidReadtextbuffered ()throwsIOException { at  -FileReader FR =NewFileReader ("Tempfile\\bufw.txt");//convert bytes to Characters -  -BufferedReader BUFR =NewBufferedReader (FR); -  -String line =NULL; in  while((Line=bufr.readline ())! =NULL){ -  toSystem.out.println (line); +  -} the  * /*string line1 = Bufr.readline ();  System.out.println (line1); PNS String line2 = Bufr.readline ();  System.out.println (line2);  the Line3 String = Bufr.readline ();  System.out.println (LINE3);  line4 String = Bufr.readline ();  System.out.println (line4);  the String line5 = Bufr.readline (); System.out.println (line5);   $  $Bufr.close (); -  -} the  -  Public Static voidWritetextbybuffered ()throwsIOException {Wuyi  the //1, clear purpose.  -FileWriter FW =NewFileWriter ("Tempfile\\bufw.txt");//Convert characters to bytes Wu  - //2, creates a buffer object. Clear the Stream object to be buffered.  AboutBufferedWriter BUFW =NewBufferedWriter (FW); $  -  for(intX=1; x<=4; X + +) { -  -Bufw.write (x+ "ABC"); ABufw.newline (); +Bufw.flush (); the} -  $ //bufw.write ("abc"); the //bufw.newline ();//system.getproperty ("Line.separator"); the //bufw.write ("Hello"); theBufw.close (); the  -} in  the} the 

2017-12-31

A message from the Java Programming Ideas Podcast Course

Java I/o---Reader & Writer (character stream)

Related Article

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.