IO流 复制文件及文件夹

  1 package io;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.io.OutputStream;
  9 import java.util.Scanner;
 10 
 11 public class copy {
 12 
 13     public static void main(String[] args) {
 14 
 15         Scanner sc = new Scanner(System.in);// 键盘输入
 16 
 17         System.out.print("请输入需要复制的源:");
 18         String srcPath = sc.next();
 19         System.out.print("请输入需要复制的目的:");
 20         String destPath = sc.next();
 21 
 22         System.out.println("复制开始");
 23         copys(srcPath, destPath);
 24         System.out.println("复制结束");
 25 
 26     }
 27 
 28     /**
 29      * 复制
 30      * 
 31      * @param srcPath
 32      *            源
 33      * @param destPath
 34      *            目的
 35      */
 36     private static void copys(String srcPath, String destPath) {
 37 
 38         File srcFile = new File(srcPath);
 39 
 40         if (srcFile.isDirectory()) {// 判断是否为文件夹
 41 
 42             File[] files = srcFile.listFiles();// 获取文件列表,File对象
 43 
 44             for (File file : files) {
 45 
 46                 String name = File.separator + file.getName();// 获取适合计算机系统的地址分隔符;获取文件名或文件夹名
 47 
 48                 if (file.isDirectory()) {// 判断是否为文件夹
 49                     copyDirectory(srcPath + name, destPath + name);
 50                 }
 51 
 52                 if (file.isFile()) {// 判断是否为文件
 53                     copyFile(file, srcPath + name, destPath + name);
 54                 }
 55 
 56             }
 57 
 58         }
 59 
 60         if (srcFile.isFile()) {// 判断是否为文件
 61             copyFile(srcFile, srcPath, destPath);
 62         }
 63 
 64     }
 65 
 66     /**
 67      * 复制文件夹
 68      * 
 69      * @param srcPath
 70      *            源
 71      * @param destPath
 72      *            目的
 73      */
 74     private static void copyDirectory(String srcPath, String destPath) {
 75 
 76         File destFile = new File(destPath);
 77 
 78         if (!destFile.exists()) {// 判断文件或文件夹是否存在
 79             destFile.mkdirs();// 创建目录(包括父目录)
 80         }
 81 
 82         copys(srcPath, destPath);
 83 
 84     }
 85 
 86     /**
 87      * 复制文件
 88      * 
 89      * @param file
 90      *            File对象
 91      * @param srcPath
 92      *            源
 93      * @param destPath
 94      *            目的
 95      */
 96     private static void copyFile(File file, String srcPath, String destPath) {
 97 
 98         InputStream is = null;
 99         OutputStream os = null;
100 
101         File srcFile = new File(srcPath);
102         File destFile = new File(destPath);
103 
104         try {
105 
106             is = new FileInputStream(srcFile);
107             os = new FileOutputStream(destFile);
108 
109             int length = (int) file.length();// 获取文件的字节长度
110             byte[] b = new byte[length];
111 
112             is.read(b);
113             os.write(b);
114 
115         } catch (IOException e) {
116 
117             System.out.println("IO异常, 复制失败");
118             e.printStackTrace();
119 
120         } finally {
121 
122             try {
123 
124                 os.close();
125                 is.close();
126 
127             } catch (IOException e) {
128 
129                 System.out.println("IO异常, 复制失败");
130                 e.printStackTrace();
131 
132             }
133 
134         }
135 
136     }
137 
138 }

猜你喜欢

转载自www.cnblogs.com/ybxxszl/p/9314096.html