读取一个字节
读到文件末尾时, read 方法返回 -1
public class aaa {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("a.txt");
//读取的文件中只有一个 a
//读取数据
int a = fis.read();
System.out.println((char)a);
int b = fis.read();// b为-1
System.out.println(b);
//最后的输出为:
//a
//-1
fis.close();
}
}
读取多个字节
用 while 循环读取
public class aaa {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("a.txt");
//读取数据
//读到文件末尾时, read 方法返回 -1
int a;
while ((a=fis.read())!=-1) {
System.out.println((char)a);
}
fis.close();
}
}
初学者,见解不足,如有错误请指出