Java 21-3 character input stream

Source: Internet
Author: User

The so-called input stream is to read the data in the file

Similarly, the character input stream InputStreamReader

4 Construction methods, but 2 are more commonly used:

Construction Method: 

A:inputstreamreader (InputStream is): reads data with default encoding
B:inputstreamreader (InputStream is,string charsetname): reads data with the specified encoding

A:inputstreamreader (InputStream is): reads data with default encoding

InputStreamReader ISR = new InputStreamReader (New FileInputStream ("A.txt"));

  

B:inputstreamreader (InputStream is,string charsetname): reads data with the specified encoding

1 New InputStreamReader (new  fileinputstream (2                 "Osw.txt"), "UTF-8");

Methods of InputStreamReader:

A * int read (): reads one character at a time
b* int Read (char[] CHS): reads one character array at a time

A * int read (): reads one character at a time

1         int ch = 0; 2          while (ch = isr.read ())! =-1) {3             System.out.print ((char) ch); 4         }56         //  release Resources 7         isr.close ();


b* int Read (char[] CHS): reads one character array at a time

1        Char[] CHS =New Char[1024];2         intLen = 0;3          while(len = Isr.read (CHS))! =-1) {4System.out.print (NewString (CHS, 0, Len));5         }6 7         //Freeing Resources8Isr.close ();

Java 21-3 character input 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.