📜  JavaFileInputStream和FileReader的区别

📅  最后修改于: 2021-09-13 02:44:49             🧑  作者: Mango

让我们首先讨论它们,以便通过示例来理解差异以解释差异。这里首先我们将讨论 FileReader 类。所以从Java的FileReader类开始用于从文件中读取数据。它以字节格式返回数据,如 FileInputStream 类。它是一个面向字符的类,用于在Java处理文件。这个类继承自 InputStreamReader 类。 FileReader 用于读取字符流。

句法:

使用文件名创建

FileReader input = new FileReader(String name);

使用文件的对象创建

FileReader input = new FileReader(File fileObj);

执行:

让我们考虑一个示例文件,其中我们使用Java FileReader 类从文本文件 Gfg.txt 中读取数据。我们假设文件中有以下数据,即“Gfg.txt”文件,如下所示:

Welcome to GeeksForGeeks.

例子

Java
// Java Progtam to Illustrate FileReader class
  
// Importing class
import java.io.FileReader;
  
// Main class
// FileReaderExample
public class GFG {
  
    // Main driver method
    public static void main(String args[]) throws Exception
    {
  
        // Creating an object of FileReader class to
        // read content from file in local directory
        FileReader fr = new FileReader("C:\\Gfg.txt");
  
        int i;
  
        // Reads from the file
        while ((i = fr.read()) != -1) {
  
            // Printing  the content inside the file
            System.out.print((char)i);
        }
  
        // Closing the connections to
        // avoid memory space
        fr.close();
    }
}


Java
// Java Program to Illustrate FileInputStream class
  
// Importing class from java.io package
import java.io.FileInputStream;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Try block to check for exceptions
        try {
  
            // Creating an object of FileInputStream class
            // in main() body to get file
            FileInputStream input
                = new FileInputStream("Gfg.txt");
  
            // Display message only
            System.out.println("Data in the file: ");
  
            // Reading the first byte
            int i = input.read();
  
            while (i != -1) {
                System.out.print((char)i);
  
                // Reads next byte from the file using
                // next() method
                i = input.read();
            }
  
            // Closing the file
            input.close();
        }
  
        // Catch block to handle the exceptions
        catch (Exception e) {
  
            // Print the exception on the console
            System.out.println(e);
        }
    }
}


输出:

Welcome to GeeksForGeeks

现在讨论第二种方式,即通过 FileInputStream 类。它存在于同一个包中,即Java.io 从文件中检索字节。它用于读取面向字节的数据(原始字节流),如图像数据、音频和视频。您还可以从字符流中读取数据。但是,建议使用 FileReader 类来读取字符流。

句法:

使用文件路径创建

FileInputStream input = new FileInputStream(stringPath);

使用文件的对象创建

FileInputStream input = new FileInputStream(File fileObject);

例子

Java

// Java Program to Illustrate FileInputStream class
  
// Importing class from java.io package
import java.io.FileInputStream;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Try block to check for exceptions
        try {
  
            // Creating an object of FileInputStream class
            // in main() body to get file
            FileInputStream input
                = new FileInputStream("Gfg.txt");
  
            // Display message only
            System.out.println("Data in the file: ");
  
            // Reading the first byte
            int i = input.read();
  
            while (i != -1) {
                System.out.print((char)i);
  
                // Reads next byte from the file using
                // next() method
                i = input.read();
            }
  
            // Closing the file
            input.close();
        }
  
        // Catch block to handle the exceptions
        catch (Exception e) {
  
            // Print the exception on the console
            System.out.println(e);
        }
    }
}

输出:

Data in the file:
Welcome to GeeksForGeeks

现在,在了解了两者之后,让我们总结一下它们之间的差异,如下表所示。

FileInputStream

FileReader

Stream is a byte-based object that can read and write bytes. Reader is Character Based, it can be used to read or write characters.
FileInputStream is Byte Based, it can be used to read bytes. FileReader is Character Based, it can be used to read characters.
Stream is used to binary input/output Reader is used to character input/output
FileInputStream is used for reading binary files. FileReader is used for reading text files in platform default encoding.
Serialization and DeSerialization can be done with FileInputStream and ObjectInputStream, and serialized objects can be saved to a file. Serialization converts an object to a byte stream, and deserialization converts it back to an object. FileReader is not used for Serialization and DeSerialization, as it reads characters not bytes.
FileInputStream is descendent of InputStream class. FileReader extends from Reader class
read() method of FileInputStream can read one byte at a time or multiple bytes in a byte array. read() method of FileReader can read one character at a time or multiple characters into an array