可能是史上最好用的 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));    }}
原创粉丝点击