Java Io study notes (9): bufferedreader and producer

Source: Internet
Author: User

If you want to receive data of any length and avoid garbled characters, you can useBufferedreaderClass

Public class bufferedreader extends Reader

Because the input data may contain Chinese characters, the complete stream is used here. Bufferedreader reads content from the buffer. All input bytes are stored in the buffer zone.

System. In itself represents inputstream (byte stream). Currently, it is required to receive a response stream. You need to convert the byte stream into a response stream.Inputstreamreader

 1   Import Java. Io. bufferedreader;
2 Import Java. Io. ioexception;
3 Import Java. Io. inputstreamreader;
4
5 Public Class Test33 {
6 Public Static Void Main (string [] ARGs) Throws Ioexception {
7 Bufferedreader reader = New Bufferedreader ( New Inputstreamreader (system. In ));
8 String STR = reader. Readline ();
9 System. Out. println (STR );
10 }
11 }



After jdk1.5, Java provides a special input data class. This class can complete the bufferedreader class function, and can also easily verify the input data. This class is stored inJava. utilPackage in progress

Use NotePad to receive input data from the keyboard:

1 ImportJava. util. vendor;
2
3 Public ClassTest34 {
4Public Static VoidMain (string [] ARGs ){
5Scanner S =NewUsing (system. In );
6String STR = S. Next ();
7System. Out. println (STR );
8}
9}

It is more convenient than directly using bufferedreader, but thisProgramThere is a problem. If the input string contains spaces, it will end. If we want to receive spaces, the separator will be changed to "\ n ".

1   Import Java. util. vendor;
2
3 Public Class Test34 {
4 Public Static Void Main (string [] ARGs ){
5 Scanner S = New Using (system. In );
6 S. usedelimiter ("\ n "); // Use Separators
7 String STR = S. Next ();
8 System. Out. println (STR );
9 }
10 }


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.