Demo06_FileCopyPlus.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package J20250725;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. /**
  7. * @author WanJl
  8. * @version 1.0
  9. * @title Demo06_FileCopyPlus
  10. * @description
  11. * @create 2025/7/25
  12. */
  13. public class Demo06_FileCopyPlus {
  14. /**
  15. * 文件复制-plus版本
  16. * @param src 开始位置
  17. * @param desc 目标位置
  18. */
  19. public static void copy(String src,String desc) throws IOException {
  20. //创建文件字节输入流
  21. FileInputStream fis=new FileInputStream(src);
  22. //创建文件字节输出流
  23. FileOutputStream fos=new FileOutputStream(desc);
  24. //创建一个byte类型数组
  25. byte[] bys=new byte[1024];
  26. int length; //每次读取的长度,每次读取多少字节
  27. //fis.read(bys):一次读取一个字节数组 该方法会返回实际读取到的字节长度
  28. //length=fis.read(bys) 每次读取的字节的长度,赋值给length。
  29. //循环读取
  30. while((length=fis.read(bys))!=-1){
  31. //System.out.println(new String(bys,0,length));
  32. fos.write(bys,0,length); //写入数据
  33. //一次写入1个字节数组(1024个字节),从0位置开始写,到长度结束
  34. }
  35. fos.close();
  36. fis.close();
  37. }
  38. public static void main(String[] args) {
  39. try {
  40. long start = System.currentTimeMillis();
  41. copy("E:/MavenRepository.zip","D:/MavenRepository.zip");
  42. long end = System.currentTimeMillis();
  43. System.out.println(end-start);
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }