ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 原理 读取一个已有的数据,并将这些读到的数据写入到另一个文件中.相比字符操作,字节流可以复制任何文件. ## 一个 一个字节复制 ~~~ //源文件 File fileInput = new File("1.jpeg"); //咪表文件 if (fileInput.exists()) { FileInputStream fileInputStream = new FileInputStream(fileInput); FileOutputStream fileOutputStream = new FileOutputStream("2.jpeg"); //保存读取到的ASCII码值 int b = 0; while ((b = fileInputStream.read()) != -1) { fileOutputStream.write(b); } fileOutputStream.close(); //先开的流后关闭 fileInputStream.close(); } else { System.out.println("文件不存在"); } ~~~ ## 字节数组复制 ~~~ //源文件 File fileInput = new File("1.jpeg"); //咪表文件 if (fileInput.exists()) { FileInputStream fileInputStream = new FileInputStream(fileInput); FileOutputStream fileOutputStream = new FileOutputStream("2.jpeg"); //保存读取到的ASCII码值 int len = 0; byte[] bs = new byte[1024]; while ((len = fileInputStream.read(bs)) != -1) { //写入bs中的从0开始len个字节到文件中 fileOutputStream.write(bs, 0, len); } fileOutputStream.close(); //先开的流后关闭 fileInputStream.close(); } else { System.out.println("文件不存在"); } ~~~