package J20250725; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * @author WanJl * @version 1.0 * @title Demo06_FileCopyPlus * @description * @create 2025/7/25 */ public class Demo06_FileCopyPlus { /** * 文件复制-plus版本 * @param src 开始位置 * @param desc 目标位置 */ public static void copy(String src,String desc) throws IOException { //创建文件字节输入流 FileInputStream fis=new FileInputStream(src); //创建文件字节输出流 FileOutputStream fos=new FileOutputStream(desc); //创建一个byte类型数组 byte[] bys=new byte[1024]; int length; //每次读取的长度,每次读取多少字节 //fis.read(bys):一次读取一个字节数组 该方法会返回实际读取到的字节长度 //length=fis.read(bys) 每次读取的字节的长度,赋值给length。 //循环读取 while((length=fis.read(bys))!=-1){ //System.out.println(new String(bys,0,length)); fos.write(bys,0,length); //写入数据 //一次写入1个字节数组(1024个字节),从0位置开始写,到长度结束 } fos.close(); fis.close(); } public static void main(String[] args) { try { long start = System.currentTimeMillis(); copy("E:/MavenRepository.zip","D:/MavenRepository.zip"); long end = System.currentTimeMillis(); System.out.println(end-start); } catch (IOException e) { e.printStackTrace(); } } }