博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java常用文件操作-1
阅读量:7117 次
发布时间:2019-06-28

本文共 4884 字,大约阅读时间需要 16 分钟。

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

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(newPath26                             + 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
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()) { //如果是文件,则添加到list28 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

你可能感兴趣的文章
2018上半年信息系统项目管理师真题
查看>>
为 Charles 添加代理页面按钮(Rewrite)
查看>>
决战燕京城-03 公司倒闭风波
查看>>
python面向对象[基础]
查看>>
如何使用榛子获得第一条短信验证码?
查看>>
Android进阶必学retrofit源码解析
查看>>
java 进销存 crm websocket即时聊天发图片文字 好友群组 SSM源码
查看>>
Day7:html和css
查看>>
什么是 5G?它比 4G 好在哪里?
查看>>
[译] C++ 和 Android 本地 Activity 初探
查看>>
Java异常简介及异常信息缺失处理
查看>>
VBA+正则表达式解决订单小问题
查看>>
字符编码ASCII、Unicode、UTF-8
查看>>
mybatis+dubbo+ springmvc+zookeeper分布式架构
查看>>
MJRefresh源码解读
查看>>
easyui tabs切换和单个页面手动刷新以及点击添加新的tabs
查看>>
一天4-5小时睡眠也可以高效工作
查看>>
图形化编程语言的设计
查看>>
实现一个前端路由,如何实现浏览器的前进与后退 ?
查看>>
面试题 async/await
查看>>