【java文件字节流复制】

时间:2024-04-07 07:54:39
import java.io.*; public class FileCopy { public static void main(String[] args) { // 源文件路径和目标文件路径 String sourceFilePath = "sourceFile.txt"; String destinationFilePath = "destinationFile.txt"; try { // 创建输入流和输出流 FileInputStream fis = new FileInputStream(sourceFilePath); FileOutputStream fos = new FileOutputStream(destinationFilePath); // 读取源文件的字节并写入目标文件 int byteRead; while ((byteRead = fis.read()) != -1) { fos.write(byteRead); } // 关闭流 fis.close(); fos.close(); System.out.println("文件复制成功!"); } catch (IOException e) { System.out.println("文件复制失败:" + e.getMessage()); } } }