当文件中只有1个字节时,FileInputStream可用方法返回2

0 java inputstream file

正如标题中所述,我试图使用FileInputSteam逐字节读取文件.我的代码是:

FileInputStream input = new FileInputStream(inFileName);
System.out.println(input.available());
Run Code Online (Sandbox Code Playgroud)

我的文件inFileName只包含字符"±",它应该只有一个字节,但是当我运行程序时,输出为2.

任何帮助是极大的赞赏.

Bla*_*are 5

这是一个unicode字符,在这种情况下是2个字节.

http://www.fileformat.info/info/unicode/char/b1/index.htm

向下滚动到UTF-8部分,您可以看到每个字节的值.

如果您的最终目标是从UTF-8的字节数组中获取字符串,那么您可以使用字节从字节生成字符串 new String(bytes, "UTF-8");

它也可能是UTF-16(也可能是2个字节),但这种情况不太常见.