Java常用文件操作-1

  在我们的实际工作中经常会用到的文件操作,再此,将工作中碰到的做一个记录,以便日后查看。

1、复制文件夹到新文件夹下

 1 /**
 2      * 复制文件夹下所有文件到指定路径
 3     *@param oldPath
 4     *@param newPath
 5     *@author qin_hqing
 6     *@date 2015年7月6日 上午11:59:33
 7     *@comment
 8      */
 9     public static void copyFolder(String oldPath, String newPath) {
10 
11         try {
12             (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
13             File a = new File(oldPath);
14             String[] file = a.list(); //获取文件夹下所有文件
15             File temp = null;
16             for (int i = 0; i < file.length; i++) {
17                 if (oldPath.endsWith(File.separator)) { //判断传入的路径是否存在路径分隔符,若没有则加上
18                     temp = new File(oldPath + file[i]);
19                 } else {
20                     temp = new File(oldPath + File.separator + file[i]);
21                 }
22 
23                 if (temp.isFile()) { //文件,则复制到目标目录
24                     FileInputStream input = new FileInputStream(temp);
25                     FileOutputStream output = new FileOutputStream(newPath
26                             + File.separator + (temp.getName()).toString());
27                     byte[] b = new byte[1024 * 5];
28                     int len;
29                     while ((len = input.read(b)) != -1) {
30                         output.write(b, 0, len);
31                     }
32                     output.flush();
33                     output.close();
34                     input.close();
35                 }
36                 if (temp.isDirectory()) {// 如果是子文件夹
37                     copyFolder(oldPath + File.separator + file[i], newPath + File.separator + file[i]);
38                 }
39             }
40         } catch (Exception e) {
41             System.out.println("复制整个文件夹内容操作出错");
42             e.printStackTrace();
43 
44         }
45 
46     }
View Code

2、删除指定文件夹下的所有文件

 1 /**
 2      * 删除该文件加下所有文件 - 该文件问空文件夹
 3      *
 4      * @param path
 5      * @return
 6      * @author qin_hqing
 7      * @date 2015年6月18日 上午11:05:00
 8      * @comment
 9      */
10     public static boolean delAllFile(String path) {
11         boolean flag = false;
12         File file = new File(path);
13         if (!file.exists()) {
14             return flag;
15         }
16         if (!file.isDirectory()) {
17             return flag;
18         }
19         String[] tempList = file.list();
20         File temp = null;
21         for (int i = 0; i < tempList.length; i++) {
22             if (path.endsWith(File.separator)) {
23                 temp = new File(path + tempList[i]);
24             } else {
25                 temp = new File(path + File.separator + tempList[i]);
26             }
27             if (temp.isFile()) {
28                 temp.delete();
29             }
30             if (temp.isDirectory()) {
31                 delAllFile(path + File.separator + tempList[i]);// 先删除文件夹里面的文件
32                 delFolder(path + File.separator + tempList[i]);// 再删除空文件夹
33                 flag = true;
34             }
35         }
36         return flag;
37     }
View Code
 1 /**
 2      * 删除该文件夹-包含子文件及文件夹
 3      *
 4      * @param folderPath
 5      * @author qin_hqing
 6      * @return
 7      * @date 2015年6月18日 上午11:04:13
 8      * @comment
 9      */
10     public static boolean delFolder(String folderPath) {
11         boolean bl = false;
12         try {
13             delAllFile(folderPath); // 删除完里面所有内容
14             String filePath = folderPath;
15             filePath = filePath.toString();
16             java.io.File myFilePath = new java.io.File(filePath);
17             myFilePath.delete(); // 删除空文件夹
18             bl = true;
19         } catch (Exception e) {
20             e.printStackTrace();
21         }
22         return bl;
23     }
View Code

3、获取指定文件夹下的所有文件列表(不包含空文件夹)

 1 /**
 2      * 获取指定目录下的所有文件路径
 3      *
 4      * @param path
 5      * @return
 6      * @author qin_hqing
 7      * @date 2015年7月3日 下午5:12:59
 8      * @comment
 9      */
10     public static List<File> getAllFile(String path) {
11         File file = new File(path);
12         if (!file.exists()) {
13             return list; //由于使用迭代,将list定义为全局变量
14         }
15         if (!file.isDirectory()) {
16             return list;
17         }
18 
19         String[] tempList = file.list();
20         File temp = null;
21         for (int i = 0; i < tempList.length; i++) {
22             if (path.endsWith(File.separator)) { //判断是否存在路径分隔符
23                 temp = new File(path + tempList[i]);
24             } else {
25                 temp = new File(path + File.separator + tempList[i]);
26             }
27             if (temp.isFile()) { //如果是文件,则添加到list
28                 list.add(temp);
29             }
30             if (temp.isDirectory()) { //如果是目录,则继续遍历
31                 getAllFile(path + File.separator + tempList[i]);// 先获取文件夹里面的文件
32             }
33         }
34         return list;
35     }
View Code

如有遗漏,后续追加...

共勉!

原文地址:https://www.cnblogs.com/edi-kai/p/4702519.html