BufferedInputStream--字节缓冲输入流和缓冲流的效率测试--复制文件
BufferedInputStream--字节缓冲输入流
public static void main(String[] args) throws IOException { //1.创建FileInputStream对象,构造方法中绑定要读取的数据源 FileInputStream fis = new FileInputStream("D:\\b.txt"); //2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率 BufferedInputStream bis = new BufferedInputStream(fis); //3.使用BufferedInputStream对象中的方法read,读取文件 //int read()从输入流中读取数据的第一个字节。 /* int len = 0; 记录每次读取的字节 while ((len=bis.read())!=-1){ System.out.println((char)len); }*/ //int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。 byte[] bytes = new byte[1024]; //存储每次读取的数据 int len=0; //记录每次读取的有效字节个数 while ((len=bis.read(bytes))!=-1){ System.out.println(new String(bytes,0,len)); } //4.释放资源 bis.close(); }
public class Demo02BufferedInputStream { public static void main(String[] args) throws IOException { //1.创建FileInputStream对象,构造方法中要绑定要读取的对象 FileInputStream fis = new FileInputStream("day10_IO\\a.txt"); //2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率 BufferedInputStream bis = new BufferedInputStream(fis); //3.使用BufferedInputStream对象中的发方法,读取文件 //int read() 从输入流中读取数据的下一个字节 /*int len = 0; // 记录每次读取到的字节 while((len = bis.read()) != -1){ System.out.println( len); }*/ //int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中 byte[] bytes = new byte[1024];//存储每次读取的数据 int len = 0; //记录每次读取到的字节 while((len = bis.read(bytes)) != -1){ System.out.println(new String(bytes,0,len)); } //4.释放资源 bis.close(); } }
缓冲流的效率测试--复制文件
/* 文件复制练习:一读一写 明确: 数据源: c:\\1.jpg 数据的目的地: d:\\1.jpg 文件复制的步骤: 1.创建字节缓冲输入流对象,构造方法中传递字节输入流 2.创建字节缓冲输出流对象,构造方法中传递字节输出流 3.使用字节缓冲输入流对象中的方法read,读取文件 4.使用字节缓冲输出流中的方法write,把读取的数据写入到内部缓冲区中 5.释放资源(会先把缓冲区中的数据,刷新到文件中) 文件的大小:780,831 字节 一次读写一个字节:32毫秒 使用数组缓冲读取多个字节,写入多个字节:5毫秒 */ public static void main(String[] args) throws IOException { long s = System.currentTimeMillis(); //1.创建字节缓冲输入流对象,构造方法中传递字节输入流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\b.txt")); //2.创建字节缓冲输出流对象,构造方法中传递字节输出流 如果有同名文件会覆盖掉他然后什么内容都没有 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\a.txt")); //3.使用字节缓冲输入流对象中的方法read,读取文件 //一次读取一个字节写入一个字节的方式 /*int len = 0; while((len = bis.read())!=-1){ bos.write(len); }*/ //使用数组缓冲读取多个字节,写入多个字节 byte[] bytes = new byte[1024]; int len = 0; while((len = bis.read(bytes))!=-1){ System.out.println(new String(bytes,0,len)); bos.write(bytes,0,len); } bos.close(); bis.close(); long e = System.currentTimeMillis(); System.out.println("复制文件共耗时:"+(e-s)+"毫秒"); } }
public static void main(String[] args) throws IOException { long s = System.currentTimeMillis(); //1.创建一个字节输入流对象,构造方法中绑定要读取的数据源 FileInputStream fis = new FileInputStream("c:\\1.jpg"); //2.创建一个字节输出流对象,构造方法中绑定要写入的目的地 FileOutputStream fos = new FileOutputStream("d:\\1.jpg"); //一次读取一个字节写入一个字节的方式 //3.使用字节输入流对象中的方法read读取文件 /*int len = 0; while((len = fis.read())!=-1){ //4.使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中 fos.write(len); }*/ //使用数组缓冲读取多个字节,写入多个字节 byte[] bytes = new byte[1024]; //3.使用字节输入流对象中的方法read读取文件 int len = 0;//每次读取的有效字节个数 while((len = fis.read(bytes))!=-1){ //4.使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中 fos.write(bytes,0,len); } //5.释放资源(先关写的,后关闭读的;如果写完了,肯定读取完毕了) fos.close(); fis.close(); long e = System.currentTimeMillis(); System.out.println("复制文件共耗时:"+(e-s)+"毫秒"); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix