The difference between Java FileInputStream and FileReader

Source: Internet
Author: User
Tags java fileinputstream

Before explaining the specifics of FileInputStream and FileReader in Java, I want to talk about the fundamental differences between InputStream and Reader in Java, and when to use InputStream and reader respectively. In fact, InputStream and reader are abstract classes and do not directly read data from a file or socket. However, the main difference between them is that InputStream is used to read binary data (Byte stream mode, translator note), reader is used to read text data (character stream mode, translator note), to be precise, Unicode characters. So what is the difference between binary data and text data? Of course, all the things that are read are essentially bytes, and then a set of character encoding schemes are required to convert the bytes into text. The reader class uses character encoding to decode bytes and return characters to the caller. The reader class either uses the default character encoding that runs the Java program platform, either using the CharSet object or a string-type character encoding name, such as "UTF-8". Although it is the simplest concept, when reading text files or reading text data from sockets, many Java developers make mistakes by not specifying character encodings. Remember that if you do not specify the correct encoding, or if your program does not use a character encoding that already exists in the protocol, such as the HTML "content-type (content Type)", the XML file header specifies the encoding, you may not be able to read all the data correctly. Some characters that are not the default encoding rendering may become "? "or a small square. Once you know the fundamental difference between stream and reader, it's easy to understand the difference between FileInputStream and FileReader. It allows you to read data from a file, whereas FileInputStream is used to read binary data and FileReader to read character data.

FileReader vs FileInputStream in Java

Because the FileReader class inherits the InputStreamReader class, the character encoding used is either provided by the class or is the default character encoding of the platform. Keep in mind that InputStreamReader will cache the character encoding. After you create the object, setting the character encoding will have no effect. Let's take a look at examples of how to use InputStream and FileReader in Java. You can provide either a file object or a string containing the file location to begin reading the character data of the file. This is similar to FileInputStream, and provides a similar constructor for reading a file source. Although it is recommended to use BufferedReader to read file data. I set my eclipse's file.encoding to UTF-8, then I create a new data.txt on the C drive and enter a perpetual word, and use Notepad to open the Save as UTF-8 code. This time we run the program in Eclipse, we can see that Data.txt's print binary content is efbbbfe6b0b8 (by notepad++ The Hex-editor plugin view data.txt file hex content can verify this), stating that FileInputStream did not perform any encoding conversion to read the binary contents of data.txt into the Java variable. Let's look at the following line of output feff6c38 will always find that FileReader read the file through UTF-8, and then encode the file so that it is converted to Unicode encoding into a Java variable so that it can be used correctly in Java. Because the variables stored in memory by Java are Unicode encoded. If we save Data.txt as ANSI (GBK) encoding, FileReader still read the file through UTF-8, then Unicode encoding of the file will cause garbled problems. If you set the eclipse's file.encoding to GBK and then run the program, it will print normally, as follows

D3c0
6c38 Yong

1234567891011121314151617181920212223242526272829303132333435363738 import java.awt.Color;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;/** * Java程序通过字节流和字符流的方式来读取文件数据。 * 需强调FileInputStream和FileReader的关键区别在于:FileReader用于读取字符流,而FileInputStream用来读取原始字节流。 * @author Javin Paul */public class HowToReadFileInJava {    public static void main(String args[]) {        // 例1 – 使用FileInputStream 读取文件内容        try (FileInputStream fis = new FileInputStream("c:/data.txt")) {            int data = fis.read();            while (data != -1) {                System.out.print(Integer.toHexString(data));                data = fis.read();            }        } catch (IOException e) {            System.out.println("Failed to read binary data from File");            e.printStackTrace();        }System.out.println ();        // 例2 – Java中使用FileReader 读取文件数据        try (FileReader reader = new FileReader("c:/data.txt")) {            int character = reader.read();            while (character != -1) {System.out.print (integer.tohexstring (character));                System.out.print((char) character);                character = reader.read();            }        } catch (IOException io) {            System.out.println("Failed to read character data from File");            io.printStackTrace();        }    }}

Save As UTF-8 output:

12345

UTF-8
Efbbbfe6b0b8
Feff6c38 Yong

Save as ANSI (GBK) output:

12345

UTF-8
D3c0
Fffd?fffd?

The 1th example is to read data in bytes from a file, so it is bound to be very slow. The read () method of the FileInputStream is blocking, reading bytes or blocks of data until no data is entered. It either returns the next byte of the data, and returns 1 when the end of the file is reached. This means that we read one byte per loop and print it as a hexadecimal string. By the way, converting a inputstream into a byte array is optional. On the other hand, example 2 reads data by character. The read () method of InputStreamReader inherited from FileReader reads a single character and returns that character, returning 1 when the end of the stream is reached. That's why the text you see in Example 2 output is exactly the same as in the file.

This is all about the difference between FileInputStream and FileReader in Java. In the final analysis: use FileReader or BufferedReader to read character or text data from a file, and always specify character encoding, and use FileInputStream to read the raw byte stream from a file or socket in Java.

The difference between Java FileInputStream and FileReader

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.