如何创建FileInputStream
类创建的字节?
例如,如果文件包含由FileInputStream
读取的数字12,生成的字节是[49,50,13,10],为什么?
import java.io.*;
public class exp{
public static void main(String[] args){
InputStream is = null;
try{
is = new FileInputStream(new File("./info.txt"));
}catch(Exception e){}
while(true){
byte b = 0;
try{
b = (byte) is.read();
}catch(Exception e){}
if(b == -1) break;
System.out.println(b);
}
}
}
12
为二进制00000000 00000000 00000000 00001100
字节应为[0, 0, 0, 12]
而不是[49, 50, 13, 10]
。
答案 0 :(得分:3)
这是ASCII。
49 == '1'
50 == '2'
13 == '\r' //carriage return
10 == '\n' //end of line
这是因为该文件是文本文件而不是二进制文件。一切都是正确的,如果文件是在Linux机器上编写的,那么它只会产生四个字符。