字节流FileInputStream FileOutputStream四种读写,包装流BufferedOutputStream BufferedInputSream 缓冲区默认8192字节

1 package test03; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class Streamtest { 10 public static void main(String[] args) throws IOException { 11 long start = System.currentTimeMillis(); 12 //test1();//2421 13 //test2();//8 14 // test3();//52 15 test4();//6 16 System.out.println(System.currentTimeMillis()-start); 17 } 18 //基本字节流,一次读写一个字节 19 public static void test1() throws IOException { 20 //封装源文件 21 FileInputStream fis = new FileInputStream("G:/Lighthouse.jpg"); 22 //封装目标文件 23 FileOutputStream fos = new FileOutputStream("G:/demo/lighthouse01.jpg"); 24 //读写 25 int b = 0; 26 while((b = fis.read())!=-1) { 27 fos.write(b); 28 } 29 //关闭资源 30 fis.close(); 31 fos.close(); 32 } 33 //基本字节流 一次一个字节数组 34 public static void test2() throws IOException { 35 FileInputStream fis = new FileInputStream("G:/Lighthouse.jpg"); 36 FileOutputStream fos = new FileOutputStream("G:/demo/lighthouse02.jpg"); 37 38 //读写 39 int len =0; 40 byte[] bys = new byte[1024]; 41 while((len = fis.read(bys))!=-1) { 42 fos.write(bys); 43 } 44 //关闭资源 45 fis.close(); 46 fos.close(); 47 } 48 //缓冲字节流 一次一个字节 49 public static void test3() throws IOException { 50 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("G:/Lighthouse.jpg")); 51 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:/demo/lighthouse03.jpg")); 52 //读写 53 int b = 0; 54 while((b = bis.read())!=-1) { 55 bos.write(b); 56 } 57 bis.close(); 58 bos.close(); 59 60 } 61 //缓冲字节流 一次一个字节数组 62 public static void test4() throws IOException { 63 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("G:/Lighthouse.jpg")); 64 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:/demo/lighthouse04.jpg")); 65 //读写 66 int len = 0; 67 byte[] bys = new byte [1024]; 68 while((len = bis.read(bys))!=-1) { 69 bos.write(bys); 70 } 71 bis.close(); 72 bos.close(); 73 74 75 76 } 77 78 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下