2
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class Main {
    public static void main(String[] args) throws IOException {
        FileReader fileReader1 = new FileReader("C:\\test\\input.txt");
        FileInputStream fileInputStream1 = new FileInputStream("C:\\test\\input.txt");
        FileReader fileReader2 = new FileReader("C:\\test\\abc.png");
        FileInputStream fileInputStream2 = new FileInputStream("C:\\test\\abc.png");

        int ab=fileReader1.read(); 
        int bc=fileInputStream1.read();

        int ab1=fileReader2.read();
        int bc1=fileInputStream2.read();

        System.out.println("reading a file : fileReader:"+ab+" fileInputStream:"+bc);
        System.out.println("resding  PNG : fileReader:"+ab1+" fileInputStream:"+bc1);
    }
}

输出:

reading a file : fileReader:104 fileInputStream:104
resding  PNG : fileReader:8240 fileInputStream:137

我正在使用 FileReader 和 FileInputStream 来读取 txt 文件和读取图像文件。我知道读取字节明智和其他字符明智。但我没有得到这个输出。

4

2 回答 2

1

文件阅读器:

读取字符文件的便利类。此类的构造函数假定默认字符编码和默认字节缓冲区大小是适当的。

文件输入流:

FileInputStream 用于读取原始字节流,例如图像数据。要读取字符流,请考虑使用 FileReader。

于 2013-05-24T19:15:14.910 回答
1

PNG 文件中的第一个字节是 0x89,即十进制的 137。FileInputStream 按原样报告字节,而 FileReader 假定它是 Windows 1252 代码页中的字符并将其转换为相应的 UTF-8 字符代码、0x2030 或 8240 十进制。(这假设您在默认代码页 1252 的 Windows 机器上运行代码)。

于 2013-05-24T20:56:36.317 回答