public class FileRead2 {
/**
* 文件-(I流+缓冲流)--》程序
* 缓冲流提高性能
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1,建立文件与程序的联系
File f=new File("D:/a.txt");
//2,选择流
BufferedInputStream/*InputStream*/ is=new BufferedInputStream(
new FileInputStream(f));
//3,操作数据
byte[] flush=new byte[31/*1024*/];
int len=0;
while(-1!=(len=is.read(flush))){
System.err.println("在这里进行数据处理"+len+"字节:"+Arrays.toString(flush));
}
is.close();
}
}
/**
* 文件-(I流+缓冲流)--》程序
* 缓冲流提高性能
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1,建立文件与程序的联系
File f=new File("D:/a.txt");
//2,选择流
BufferedInputStream/*InputStream*/ is=new BufferedInputStream(
new FileInputStream(f));
//3,操作数据
byte[] flush=new byte[31/*1024*/];
int len=0;
while(-1!=(len=is.read(flush))){
System.err.println("在这里进行数据处理"+len+"字节:"+Arrays.toString(flush));
}
is.close();
}
}
本文介绍了一种通过Java中的缓冲流(BufferedInputStream)来高效读取文件的方法。该方法首先创建了文件(File)与程序之间的连接,接着选择了合适的输入流进行数据读取,并利用缓冲区提高读取效率。
355

被折叠的 条评论
为什么被折叠?



