从磁盘读取一个文件到内存中,再打印到控制台
public class work3 {
public static void main(String[] args) throws IOException {
getFile();
}
//通过建立缓冲区和循环的方式来读取
public static void getFile() throws IOException{
//1.找目标文件
File file = new File("C:\\abcd\\12.5.txt");
//2.建立通道
FileInputStream fileInputStream = new FileInputStream(file);
//3.创建缓冲区
byte[] b = new byte[1024];
//4.读取数据
int count = 0;
while((count = fileInputStream.read(b))!=-1){
System.out.println(new String(b,0,count));
//是说把数组b中的元素付给String,0是指偏移量,从数组第0个开始
}
fileInputStream.close();
}
}