Java InputStream and Reader

Source: Internet
Author: User

1. The difference between a byte stream and a character stream:

The data unit for a byte stream operation is a 8-bit byte, and the data unit of a character stream operation is a 16-bit character.

2. The difference between a node stream and a processing stream:

A stream of data that can be read and written from/to a specific IO device (such as a disk, a network), called a node stream, is also known as a low-level stream.

The processing stream is used to connect or encapsulate an already existing stream, and to implement the data read and write function through the encapsulated stream.

3. InputStream and Reader

InputStream and Reader are abstract base classes for all input streams and cannot be used to create instances or perform inputs, but to be templates for all input streams, methods that are available to all input streams.

InputStream contains the following methods:

// reads a single byte from the input stream and returns the byte data read int read ();     // reads up to b.length bytes of data from an input stream and stores it in byte array B, returning the actual number of bytes read int Read (byte  b[]); // reads a maximum of Len characters from the input stream and stores it in the character array buff, not from the beginning of the array starting from the off position. int Read (byteintint len)

Reader includes the following methods:

// reads a single character from the input stream, returning the character data that was read int read (); // reads up to cbuf.length character data from the input stream and stores it in Cbuf, returning the actual number of characters read int Read (char  cbuf[]); // reads up to cbuf.length character data from the input stream, stores it in Cbuf, stores it from off, returns the number of characters actually read int Read (charintint len);

InputStream and reader are abstract base classes, they cannot create instances themselves, but they each have an input stream for reading the file: FileInputStream, FileReader

 Public classFileinputstreamtest { Public Static voidMain (string[] args) {InputStream fis=NULL; Try{FIS=NewFileInputStream ("D:/rding/testfile2/file.txt"); byte[] Bbuf =New byte[1024]; intHasread = 0;  while((Hasread = Fis.read (bbuf)) > 0) {System.out.println (NewString (bbuf, 0, Hasread)); }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {            Try {                if(FIS! =NULL) {fis.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }    }}
 Public classFilereadertest { Public Static voidMain (string[] args) {Reader fr=NULL; Try{FR=NewFileReader ("D:/rding/testfile2/file.txt"); Char[] Cbuf =New Char[32]; intHasread = 0;  while((Hasread = Fr.read (cbuf)) > 0) {System.out.print (NewString (cbuf, 0, Hasread)); }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {            if(FR! =NULL) {                Try{fr.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }}

  

    

    

  

Java InputStream and Reader

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.