import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
try {
//找到目标文件
File file = new File("D:\\test.txt");
//建立数据的输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立缓冲数组配合循环读取文件的数据。
int length = 0; //保存每次读入缓冲区的字节总数
byte[] buf = new byte[1024]; //存储读取到的数据
StringBuilder StringBuilder = new StringBuilder();
while((length = fileInputStream.read(buf))!=-1){ // read方法如果已经到达文件末尾而没有更多的数据,则返回 -1。
StringBuilder.append(new String(buf,0,length));
}
System.out.println("test.txt文件内容是:\n" + StringBuilder.toString());
//关闭资源
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}