Skip to content

How different between FileInputStream and FileReader #5

@lyj289

Description

@lyj289

在JAVA中有多种方式读取文件,比如FileReader FileInputStream,其有什么差别呢?

假设有一文件hello.txt,其内容为:

没错,只有一个中文字符,为了方便对比结果。

其中,中文字符 的UNICODE 为 26446, 利用UTF-8编码,可用3个字节表示, 16进制表示为E69D8E

字符 Unicode编码(10进制) Unicode编码(16进制) UTF-8编码(10进制) UTF-8编码(16进制)
26446 674E 15113614 E69D8E

分别用以下两种方式读取

  public static void read1() throws IOException{
    FileReader input = new FileReader("hello.txt");
    int n;
    while ((n = input.read()) != -1) { // 利用while同时读取并判断
        System.out.println((char) n);
		System.out.println(n);
    }
  }

  public static void read2() throws IOException{
    InputStream input = new FileInputStream("hello.txt");
    int n;
    while ((n = input.read()) != -1) { // 利用while同时读取并判断
        System.out.println(n);
    }
  }

输出结果

Output of read1 by FileReader:

李 (正确输出中文)
26446

Output of read2 by FileInputStream:

230
157
142

输出3个number,即的utf-8编码 E6 9D 8E 的10进制形式。

,是中文字符,用UTF-8编码存储,占3个byte,用FileReader读取时,默认系统编码UTF-8,读取结果为单一字符,符合预期,如果用FileInputStream读取,将以一个byte一个byte的形式读取,结果会读到3个byte, 不符合预期。

当然,如果换成是英文字符,UTF-8存储是一个字节,和ASCII编码表示一样,刚两次输出结果将会是相同的, 因此FileReader的一个优势是处理非ASCII编码外的多字节表示编码。

由结果,可知:

  • FileReader 一个一个字符进行读取
  • FileInputStream 一个一个byte进行读取
  • 当文件内容全为ASCII编码时,两者处理无差别
  • 当文件内容为UTF-8等多字节编码时,两者处理有差异

参考

Metadata

Metadata

Assignees

No one assigned

    Labels

    dailydaily update

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions