以下代码给出了存储char
到byte
. 如何将fin.read()
函数读取的字节存储到字节数组中并打印出来?
import java.io.FileInputStream;
class IO {
public static void main(String args[]) {
int i;
int j = 0;
FileInputStream fin = new FileInputStream("file.txt");
byte[] b = new byte[100];
do {
i = fin.read();
j++;
b[j] = (char) i;
} while (i != -1);
System.out.println(b);
}
}
输出:
possible loss of precision
found : char
required: byte
b[j] =(char) i;
^
1 error
我如何使它工作?将文件读入字节数组然后显示?