The difference between InputStream and reader in Java

Source: Internet
Author: User
Tags readline

Turn from: 51426440

java.io There are two abstract classes: InputStream and Reader
InputStream is a superclass of all classes that represent byte input streams
Reader is an abstract class for reading stream of characters
The InputStream provides a byte stream reading, not a text read, which is fundamentally different from the reader class.
That is, read by reader is a char array or string, using InputStream to read a byte array.
Figure out the fundamental differences between the two superclass, and then look at the use of sub-categories below them, here are only a few of the most commonly used instructions

InputStream
| __fileinputstream

FileInputStream gets the input bytes from a file in the file system.
Construction Method Summary
FileInputStream (File file)
Create a FileInputStream by opening a connection to the actual file, which is specified through the file-object files in the filesystem.
FileInputStream (FileDescriptor fdobj)
Create a FileInputStream by using the file descriptor Fdobj, which represents an existing connection to an actual file in the file system.
FileInputStream (String name)
Create a FileInputStream by opening a connection to the actual file, which is specified by the pathname name in the file system.


Reader

|--bufferedreader
|___inputstreamreader
|__filereader


BufferedReader: Reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows.

Construction Method Summary
BufferedReader (Reader in)
Creates a buffered character input stream that uses the default size input buffer.
BufferedReader (Reader in, int sz)
Creates a buffered character input stream that uses the specified size input buffer.
BufferedReader (Java Platform SE 6)
The biggest feature of BufferedReader is the setting of the buffer. Typically, every read request made by reader causes a corresponding read request to the underlying character or byte stream, and if there is no buffer, each call to read () or readLine () causes the bytes to be read from the file and converted to characters, which is extremely inefficient.
You can use BufferedReader to specify the size of the buffer, or you can use the default size. In most cases, the default value is large enough.
Therefore, it is recommended to use BufferedReader to wrap all of their read () operations with potentially expensive Reader (such as FileReader and InputStreamReader). For example
BufferedReader in
= new BufferedReader (New FileReader ("foo.in"));
The input for the specified file is buffered.
InputStreamReader (Java Platform SE 6)
InputStreamReader is a bridge of byte flow to a character stream: it reads the bytes with the specified charset and decodes them to characters. The character set it uses can be specified or explicitly given by name, or it can accept the default character set of the platform.

Construction Method Summary
InputStreamReader (InputStream in)
Create a inputstreamreader that uses the default character set.
InputStreamReader (InputStream in, Charset CS)
Creates a inputstreamreader using the given character set.
InputStreamReader (InputStream in, Charsetdecoder Dec)
Creates a inputstreamreader using the given character set decoder.
InputStreamReader (InputStream in, String CharsetName)
Creates a inputstreamreader that uses the specified character set.

Each call to a read () method in InputStreamReader causes one or more bytes to be read from the underlying input stream. To enable a valid conversion from byte to character, you can read more bytes in advance from the underlying stream to exceed the bytes required to satisfy the current read operation.
In order to achieve maximum efficiency, it is necessary to consider packaging InputStreamReader within the BufferedReader. For example:
BufferedReader in = new BufferedReader (new InputStreamReader (system.in));
InputStreamReader The biggest feature is the fixed encoding format that can refer to the conversion
, which other classes cannot, can be seen from the construction method,
This is useful when reading Chinese characters

FileReader
1) FileReader class Introduction:
Subclasses of the InputStreamReader class, all methods (read (), etc.) are inherited from the parent class InputStreamReader;
2) Differences from the InputStreamReader class:
Construction Method Summary
FileReader (File file)
Creates a new filereader given the File from which the data is read.
FileReader (FileDescriptor FD)
Creates a new filereader given the filedescriptor from which the data is read.
FileReader (String fileName)
Creates a new FileReader given the file name from which the data is read

The main difference between this class and its parent class InputStreamReader is the constructor function, which is mainly the constructor!
As seen from the InputStreamReader constructor, the parameters are InputStream and encoded, and you can see that
You must use the InputStreamReader when you want to specify the encoding method
While the parameters of the FileReader constructor are the same as the FileInputStream, which is a file object or a string representing path, you can see that the filereader is used when you want to read a document according to the files object or string;
I think the role of FileReader sub-category is the small division of labor. This class is inputstreamreader with its parent class
The main difference is the constructor function, the main differences are in the constructor!
From InputStreamReader
As seen in the constructor, the parameters are InputStream and encoded, and you can see that
You must use the InputStreamReader when you want to specify the encoding method
class, while the parameters and FileInputStream of the FileReader constructor
As a file object or a string representing path, you can see that the filereader is used when you want to read a document according to the files object or string;
I think the role of FileReader sub-category is the small division of labor.
Two relations and differences
(1) characters and bytes:
The FileInputStream class takes binary input/output, I/O speed and efficiency, but its read () method reads a byte (binary data), which is not good for people to read, and can not directly manipulate characters in the file, such as replace, find (must act in bytes) ;
While the reader class compensates for this flaw, it is very convenient to input/output in text format, for example, you can use the while ((ch = filereader.read ())!=-1) loop to read the file; You can use BufferedReader's ReadLine () Method reads the text one line at a line.
(2) coding
InputStreamReader, which is a bridge in which bytes are converted to characters. You can specify the encoding in the constructor, and if you do not specify it, the default encoding for the underlying operating system, such as GBK, is used.
FileReader and InputStreamReader involve encoding conversion (specifying encoding or using OS default encoding), which may appear garbled on different platforms! The FileInputStream is processed in binary mode, and there is no garbled phenomenon.
Therefore, to specify the encoding method, the InputStreamReader class must be used, so it is a bridge in which bytes are converted to characters;
(3) Buffer
The Bufferreader class is used to wrap all Reader (such as FileReader and InputStreamReader) whose read () operations may be expensive.
(4) Specification usage
Summarize the above, and draw a good standard usage:
1) file File = new file ("Hello.txt");
FileInputStream in=new fileinputstream (file);
2) file File = new file ("Hello.txt");
FileInputStream in=new fileinputstream (file);
InputStreamReader inreader=new InputStreamReader (in, "UTF-8");
BufferedReader bufreader=new BufferedReader (Inreader);
3) File File = new file ("Hello.txt");
FileReader filereader=new filereader (file);
BufferedReader bufreader=new BufferedReader (FileReader);

The difference between InputStream and reader in Java

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.