原生Java代码拷贝目录

    拷贝、移动文件(夹),有三方包commons-io可以用,但是有时候有自己的需求,只能使用原生java代码,这时可以用以下几种方式进行拷贝:

1、使用系统命令(Linux)调用

    此种方式对操作系统有要求,好处是代码量少,性能可依赖操作系统优化,但是中间环节不可控。

 1     /**
 2      * 执行命令
 3      */
 4     private static void nativeCall(String... cmd) {
 5         ProcessBuilder pb = new ProcessBuilder(cmd);
 6         try {
 7             pb.redirectErrorStream(true);
 8             Process process = pb.start();
 9             InputStream stream = process.getInputStream();
10             byte[] buff = new byte[4096];
11             while (stream.read(buff, 0, 4096) != -1) {}
12             stream.close();
13         } catch (IOException e) {
14             e.printStackTrace();
15         }
16     }
17 
18     // 使用CMD拷贝目录
19     public static void cmdCopyDir(File src, File des) {
20         if (!des.exists()) {
21             des.mkdirs();
22         }
23         String[] cmds = new String[] { "sh", "-c", "cp -a "" + src.getAbsolutePath() + "/." "" + des.getAbsolutePath() + """ };
24         Executor.nativeCall(cmds);
25     }
26 
27     // 使用CMD删除目录
28     public static void cmdDelDir(File dic) {
29         if (dic.exists() && dic.isDirectory()) { // 判断是文件还是目录
30             String[] cmds = new String[] { "sh", "-c", "rm -rf "" + dic.getAbsolutePath() + """ };
31             Executor.nativeCall(cmds);
32         }
33     }

2、使用文件通道拷贝

    此种方式是常用的拷贝方式,大部分环节、异常都是可控的,具体的文件复制则使用了通道的方式进行加速。

 1     // 拷贝目录
 2     public static void javaCopyDir(File src, File des) throws IOException {
 3         if (!des.exists()) {
 4             des.mkdirs();
 5         }
 6         File[] file = src.listFiles();
 7         for (int i = 0; i < file.length; ++i) {
 8             if (file[i].isFile()) {
 9                 channelCopy(file[i], new File(des.getPath() +  "/" + file[i].getName()));
10             } else if (file[i].isDirectory()) {
11                 javaCopyDir(file[i], new File(des.getPath() +  "/" + file[i].getName()), stoppable);
12             }
13         }
14     }
15 
16     // 拷贝文件
17     private static void channelCopy(File src, File des) throws IOException {
18         FileInputStream fi = null;
19         FileOutputStream fo = null;
20         FileChannel in = null;
21         FileChannel out = null;
22         IOException ex = null;
23         try {
24             fi = new FileInputStream(src);
25             fo = new FileOutputStream(des);
26             in = fi.getChannel();
27             out = fo.getChannel();
28             in.transferTo(0, in.size(), out); // 连接两个通道,并且从in通道读取,然后写入out通道
29         } finally {
30             try {
31                 fi.close();
32                 in.close();
33             } catch (IOException e) {
34                 ex = e;
35             }
36             try {
37                 fo.close();
38                 out.close();
39             } catch (IOException e) {
40                 ex = e;
41             }
42         }
43         if (ex != null) {
44             throw ex;
45         }
46     }
47        

 3、原生删除目录

 1     // 删除目录
 2     private static void javaDelDir(File dic) throws IOException {
 3         if (dic.exists() && dic.isDirectory()) {
 4             File delFile[] = dic.listFiles();
 5             if (delFile.length == 0) { // 若目录下没有文件则直接删除
 6                 dic.delete();
 7             } else {
 8                 for (File file : delFile) {
 9                     if (file.isDirectory()) {
10                         javaDelDir(file); // 递归调用del方法并取得子目录路径                        
11                     }
12                     file.delete(); // 删除文件
13                 }
14                 dic.delete();
15             }
16         }
17     }

    转载请注明原址:http://www.cnblogs.com/lekko/p/8472353.html 

原文地址:https://www.cnblogs.com/lekko/p/8472353.html