The difference between InputStreamReader and FileReader and the difference between InputStream and Reader

  • 2020-04-01 04:32:22
  • OfStack

First, I'll introduce the difference between InputStreamReader and FileReader, as follows:

InputStreamReader and BufferedReader. The most important of these classes is InputStreamReader, which is a bridge between bytes and characters. You can respecify the encoding in the constructor, otherwise the default encoding of the underlying operating system, such as GBK, will be used.

FileReader and InputStreamReader involve coding transformations (either by specifying the encoding or by using the OS default encoding), which can cause confusion on different platforms! The FileInputStream is processed in binary mode, and there is no garbled code.

The FileInputStream stream is read in bytes.

The FileReader character stream is read character by character.


BufferedReader bufReader = null;
InputStreamReader isr = null;
FileReader fr = null;
try {
for(String fileName:fileNames){
   Methods a :
  isr = new InputStreamReader(new FileInputStream("D:test.txt"), "utf-8");
  bufReader = new BufferedReader(isr);
   Method 2 :
  fr = new FileReader("D:test.txt");
  bufReader = new BufferedReader(fr);
  while (bufReader.ready()) {
   //1. Get every row of data
   String dataLine = bufReader.readLine();
   }
}

The difference between an InputStream and a Reader

Below java.io are two abstract classes: InputStream and Reader

An InputStream is a superclass that represents all the classes of the byte InputStream

Reader is an abstract class for reading a stream of characters

InputStream provides byte stream reading, not text reading, which is the fundamental difference from the Reader class.

Reader reads out a char array or String, InputStream reads out a byte array.

Having made the fundamental difference between the two superclasses, let's look at the use of their underlying subclasses

InputStream
    | __FileInputStream

A FileInputStream takes input bytes from a file in the file system.

Construction method abstract  

FileInputStream (File  The file)  

                  Create a FileInputStream by opening a connection to the actual File, which is specified by the File object File in the File system.

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 the text from the character input stream, buffering the individual characters to enable efficient reading of characters, arrays, and lines.

Construction method abstract  

BufferedReader (Reader  In)

                  Creates a buffered character input stream using 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 a BufferedReader is the setting of the buffer. Typically, every read request made by the Reader results in a corresponding read request to the underlying character or byte stream, and if there is no buffering, every call to read() or readLine() results in a byte being read from the file, converted to a character, and returned, which is extremely inefficient.

Use a BufferedReader to specify the size of the buffer or to use the default size. In most cases, the default value is large enough.

Therefore, it is recommended that a BufferedReader be used to wrap all readers whose read() operations might be expensive (such as FileReader and InputStreamReader).

For example,


 BufferedReader in
  = new BufferedReader(new FileReader("foo.in"));

  Buffered the input to the specified file.


InputStreamReader (Java Platform SE 6) 

The InputStreamReader is the bridge between the byte stream and the character stream: it reads the byte using the specified charset and decodes it into a character. The character set it USES can be specified by name or given explicitly, or it can accept the platform's default character set.  

Construction method abstract  


InputStreamReader (InputStream in) 
      Create one that USES the default character set  InputStreamReader .  
InputStreamReader (InputStream in, Charset cs) 
      Creates one that USES the given character set  InputStreamReader .  
InputStreamReader (InputStream in, CharsetDecoder dec) 
      Creates a decoder that USES the given character set  InputStreamReader .  
InputStreamReader (InputStream in, String charsetName) 
      Creates one that USES the specified character set  InputStreamReader . 
 

Each call to a read() method in InputStreamReader causes one or more bytes to be read from the underlying input stream. To enable efficient conversion from bytes to characters, more bytes can be read from the underlying stream ahead of time, making it more than necessary for the current read operation.

For maximum efficiency, consider wrapping InputStreamReader inside a BufferedReader. Such as:


 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

The biggest feature of InputStreamReader is that it can refer to the fixed encoding format of the transformation, which is not available in other classes. As you can see from the constructor, this is very useful when reading Chinese characters

FileReader

1) introduction to the FileReader class:

Subclasses of InputStreamReader, all methods (read (), etc.) are inherited from the parent InputStreamReader.

2) differences from the InputStreamReader class:

Construction method abstract  

FileReader (File  The file)  

                  Create a new FileReader given the File from which the data is read.

FileReader (FileDescriptor  Fd)  

                Create a new FileReader given a FileDescriptor that reads the data from it.

FileReader (String  FileName)  

                  Create a new FileReader  given the filename of the file from which the data is read;

The main difference between this class and its parent InputStreamReader is the constructor, and the main difference is the constructor!

As you can see from the constructor of InputStreamReader, the arguments are InputStream and encoding, so you must use the InputStreamReader class to specify encoding. The argument of the FileReader constructor is the same as the FileInputStream, which is a File object or a String for path.

I guess that's where the FileReader subclass comes in. The main difference between this class and its parent InputStreamReader is the constructor, and the main difference is the constructor!

From InputStreamReader

As you can see from the constructor of "InputStream", the arguments are InputStream and encoding method. As you can see, InputStreamReader class must be used when specifying encoding method. The argument of the FileReader constructor is the same as the FileInputStream, which is a File object or a String for path.

I guess that's where the FileReader subclass comes in.

Connection and distinction

(1) characters and bytes:

The FileInputStream class is fast and efficient with binary input/output, I/O, but its read () method reads a byte (binary data), which is difficult for people to read, and it cannot directly manipulate the characters in the file, such as replace, find (must operate in bytes);

The Reader class makes up for this by providing input/output in text format, which is very convenient. For example, you can use while((ch = filereader.read())! =-1) loop to read the file; You can use the BufferedReader's readLine() method to read the text line by line.

(2) coding

InputStreamReader, which is a byte to character bridge. You can respecify the encoding in the constructor, otherwise the default encoding of the underlying operating system, such as GBK, will be used.

FileReader and InputStreamReader involve coding transformations (either by specifying the encoding or by using the OS default encoding), which can cause confusion on different platforms! The FileInputStream is processed in binary mode, and there is no garbled code.

Therefore, the InputStreamReader class must be used to specify the encoding, so it is a byte to character bridge.

(3) the cache area

      The BufferReader class is used to wrap all readers whose read() operations might be expensive (such as FileReader and InputStreamReader).

(4) standard usage

Summarize the above contents and get a better standard usage:

1) File = new File ("hello.txt");


FileInputStream in=new FileInputStream (file); 

2) File = new File ("hello.txt");


FileInputStream in=new FileInputStream (file); 
InputStreamReader inReader=new InputStreamReader (in,"UTF-8"); 
BufferedReader bufReader=new BufferedReader(inReader); 

3) File = new File ("hello.txt");


FileReader fileReader=new FileReader(file); 
BufferedReader bufReader=new BufferedReader(fileReader);

Related articles: