import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 测试类, 字节输入流FileInputStream
* @author Administrator
*
*/
public class Test2 {
public static void main(String[] args) {
try {
//创建IO流对象
InputStream is = new FileInputStream("file/test.txt");
//创建辅助变量
int unit = -1; //一个单元的数据
//循环, 读取文件内容
while(true) {
//读取1个字节的数据
unit = is.read();
//判断, unit
if (unit == -1) break;
//打印读取到的内容
System.out.print((char)unit);//如果有中文,无法正常输出
}
//关闭IO资源
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
file/test.txt文件中的内容为:
运行结果: