1 package file; 2 3 import java.io.BufferedInputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 8 public class Demo3 { 9 public static void main(String[] args) throws IOException { 10 readTest(); 11 } 12 13 //BufferedInputStream维护的是一个字节数组,8192 14 //凡是缓冲流,都不具备读写文件的能力 15 public static void readTest() throws IOException { 16 File file = new File("F:\\2.txt"); 17 FileInputStream fileInputStream = new FileInputStream(file); 18 //建立缓冲输入字节流 19 //因为BufferedInputStream不具备读写文件的能力,要借助FileInputStream 20 BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); 21 22 //读取文件数据 23 int content = 0; 24 while((content = bufferedInputStream.read()) != -1) { 25 System.out.println((char)content); 26 } 27 28 //关闭资源 29 bufferedInputStream.close(); //实际上关闭的FileInputStream的close 30 } 31 }
时间: 2024-11-08 03:38:06