The difference between FileInputStream and FileReader is particularly detailed

There are only two kinds of operations for the data in the file: read and write
For Java, the operation of data is very important

Byte stream (FileOutputStream, FileInputStream) and character stream (FileWriter, FileReader)

In fact, the two methods are similar. They both write data or read data from files,

The difference between them is that byte stream operates in bytes, while character stream operates in characters.

If we store such a sentence in a text document:

321 love is like blue sky and white clouds, clear sky, sudden storm!
1
There are numbers, Chinese characters and punctuation marks in this sentence,
Number: 1 byte
Chinese characters: 2 bytes
Chinese punctuation: 2 bytes
English punctuation: 1 byte
And they all belong to one character

If we use byte stream to read files, part of the code is as follows

FileInputStream in;

       try {
            in=new FileInputStream(file);
            byte bytRead[]=new byte[1024];
            int len=in.read(bytRead);
            System.out.println("The contents of the document are:"+new String(bytRead,0,4));
            //Note that here we only read the first four bytes of the file
            in.close();
        }catch(IOException e) {
            e.printStackTrace();
        }



The output result is:

The contents in the file are: 321?

We found that the first three numbers can be output normally, but the "I" in the text is not output normally, but a "?" is output, This is the characteristic of byte stream. It operates in bytes. It only outputs the first four bytes, while "321 I" is five bytes, so "I" is not output normally

Then let's look at the operation of character stream. Part of the code is as follows

FileReader fr=null;

        try {
            fr=new FileReader(file);
            char chRead[]=new char[1024];
            int hasread=-1;
            try {
                    while((hasread=fr.read(chRead))!=-1) {//hasread indicates the number of characters in the text
                    System.out.println("The contents of the document are:"+new String(chRead,0,hasread));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



Output results:

The content in the file is: 321 love

We found that when outputting, the parameters passed in are also 4, and when we operate with the character stream, we can output the original data completely
Byte stream:

System.out.println("the content in the file is:" + new String(bytRead,0,4));

Character stream:

System.out.println("the content in the file is:" + new String(chRead));

The above is the main difference between byte stream and character stream. The same is true for FileOutputStream and FileWriter, but they both write to files. We just need to know that the units they operate are different:
Byte stream: data operations are performed in bytes
Character stream: data operations are performed in character units

Note:
1. When we use byte stream to operate, we create byte array to store the read data

byte bytRead[]=new byte[1024];

When we use the character stream to operate, we create a character array to store the read data

char chRead[]=new char[1024];

2. Be sure to distinguish which is write and which is read
write in:
FileOutputStream
FileWriter
Read out:
FileInputStream
FileReader
--------
Copyright notice: This article is the original article of CSDN blogger "Yiling Time", which follows the CC 4.0 BY-SA copyright agreement. For reprint, please attach the source link of the original text and this notice.
Original link: https://blog.csdn.net/qq_36554582/article/details/82108672

Keywords: Java

Added by Tekime on Fri, 14 Jan 2022 19:27:03 +0200