可能是史上最好用的 Java 文件操作帮助类
来源:互联网 发布:凯文先生的淘宝店 编辑:程序博客网 时间:2024/06/05 16:46
package com.lb.util;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.channels.FileChannel;import java.util.ArrayList;import java.util.List;public class FileHelper { private List<String> filePathList = new ArrayList<>(); /** * 获取一个路径下的所有文件(包含子文件夹) * * @param rootPath */ public List<String> getAllFilePath(String rootPath) { File file = new File(rootPath); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { getAllFilePath(files[i].getPath()); } else { filePathList.add(files[i].getPath()); } } return filePathList; } /** * 获取一个路径下的所有文件(包含子文件夹) 可以指定过滤规则 * * @param rootPath */ public List<String> getAllFilePath(String rootPath, FileFilter fileFilter) { File file = new File(rootPath); File[] files = file.listFiles(fileFilter); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { getAllFilePath(files[i].getPath(), fileFilter); } else { filePathList.add(files[i].getPath()); } } return filePathList; } /** * 删除空目录 * @param dir 将要删除的目录路径 */ public static void doDeleteEmptyDir(String dir) { boolean success = (new File(dir)).delete(); if (success) { System.out.println("Successfully deleted empty directory: " + dir); } else { System.out.println("Failed to delete empty directory: " + dir); } } /** * 递归删除目录下的所有文件及子目录下所有文件 * @param dir 将要删除的文件目录 * @return boolean Returns "true" if all deletions were successful. * If a deletion fails, the method stops attempting to * delete and returns "false". */ public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // 目录此时为空,可以删除 return dir.delete(); } /** * 使用文件通道的方式复制文件 * * @param srcPath * 源文件 "F:/期刊导航数据/学术之家/21世纪/54431b8d_92cf.jpg" * @param descPath * 复制到的新文件 "F:/期刊导航数据/out */ public static void copy(String srcPath, String descPath) { File s = new File(srcPath); File t = new File(descPath + "/" + s.getName()); FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel();// 得到对应的文件通道 out = fo.getChannel();// 得到对应的文件通道 in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 使用文件通道的方式复制文件 * @param srcFile * 源文件 源文件 "F:/期刊导航数据/学术之家/21世纪/54431b8d_92cf.jpg" * @param descPath * 目标路径 "F:/期刊导航数据/out" * */ public static void copy(File srcFile, String descPath) { File s = srcFile; File t = new File(descPath + "/" + s.getName()); FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel();// 得到对应的文件通道 out = fo.getChannel();// 得到对应的文件通道 in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 对当前文件夹下的文件改名 * @param oldFilePath * @param newName */ public static void reName(String oldFilePath, String newName) { String basePath = oldFilePath.substring(0, oldFilePath.lastIndexOf("/")); String descPaht =basePath + "/" + newName; reNameTo(oldFilePath, descPaht); } /** * 对当前文件夹下的文件改名 * @param oldFilePath * @param newName */ public static void reName(File oldFile, String newName) { String oldFilePath = oldFile.getPath(); String basePath = oldFilePath.substring(0, oldFilePath.lastIndexOf("\\")); String descPaht =basePath + "/" + newName; reNameTo(oldFile, descPaht); } /** * 在目标目录下创建 文件夹 * @param descPath "C:\Users\Administrator\Desktop\工作\编码测试\pptImgs\[推广方案ppt-www.1ppt.com]" * @param dirName "image" */ public static void makeDir(String descPath, String dirName) { File file = new File(descPath); if (file != null) { new File(descPath + "/" + dirName).mkdir(); } else { System.out.println("目标目录不为空"); } } private static void reNameTo(File oldFile, String newFilePath) { oldFile.renameTo(new File(newFilePath)); } private static void reNameTo(String oldFilePath, String newFilePath) { File oldFile = new File(oldFilePath); oldFile.renameTo(new File(newFilePath)); }}
阅读全文
0 0
- 可能是史上最好用的 Java 文件操作帮助类
- 最好用的C#操作ini文件的类库
- 最好的apusic帮助文档
- Android 文件操作帮助类FileUtils文件
- DateTools,可能是最好用的iOS日期工具库
- DateTools,可能是最好用的iOS日期工具库
- DateTools,可能是最好用的iOS日期工具库
- 最好用的PHP数据库操作类-ezSQL
- echars java帮助类使用操作实现
- 文件管理-最好用的文件管理器
- 如何帮助自己做最好的自己?
- 最好的教程竟是帮助文档
- 一份可能是最好的销售手册
- 可能是CAP理论的最好解释
- 可能是CAP理论的最好解释
- 可能是东半球最好的dagger2文章
- 可能是CAP理论的最好解释
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- varnish
- redis设计与实现(六)整数集合
- 静态库与动态库使用与区别
- 深入理解JVM之虚拟机类加载机制
- 如何简单使用Git上传本地项目到github
- 可能是史上最好用的 Java 文件操作帮助类
- Windows编程基础--第17节 MFC之状态栏(CStatusBar)
- 基于node的websocket学习笔记二:一个简单的聊天室程序与优化方案
- 自定义view的矩形 阶梯状 画笔 画布
- Eclipse
- AsyncTask异步工具类
- Nginx简介
- 总结
- java的基本算法