Java File工具类源码分享

来源:互联网 发布:mac ssh使用教程 编辑:程序博客网 时间:2024/06/05 08:44

分享一个本人编写的Java文件工具类,实现文件复制,文件移动,文件重命名,文件夹复制,文件夹移动,文件夹修改,文件夹下文件查询等功能

package com.zhiwei.io;import java.io.*;import java.util.ArrayList;import java.util.List;public final class FileUtil {    /**保存特定后缀文件绝对路径列表:设置为static属性解决迭代文件名无法累加保存的问题*/    public static List<String>  specialFileNames=new ArrayList<String>();    public static void main(String[] args) {        System.out.println(queryFileByDirName("d:/我的音乐", ".mp3"));    }    /**     * 搜索文件夹下的特定文件     * @param dirPath     * @param suffix     * @return     */    public static List<String> queryFileByDirName(String dirPath,String suffix){        File file=new File(dirPath);        if(!file.exists()){            return new ArrayList<String>();        }        File[] allFiles=file.listFiles();        File tempFile=null;        for(int i=0;i<allFiles.length;i++){            tempFile=allFiles[i];            if(tempFile.isFile() && tempFile.getAbsolutePath().endsWith(suffix)){                   specialFileNames.add(dirPath+File.separator+tempFile.getName());                         }            if(tempFile.isDirectory()){                   queryFileByDirName(dirPath+File.separator+tempFile.getName(), suffix);                           }        }        return specialFileNames;    }    /**     * 删除文件     * @param sourcePath:源文件     * @param destinationPath:目标文件     * @category 源文件-->缓存--->目标文件     */    public static  boolean copyFile(String sourcePath,String destinationPath){        //路径一致不复制        if(sourcePath.equals(destinationPath)){            return true;        }        boolean flag=false;        File file=new File(sourcePath);        //源文件不存在则中断执行        if(!file.exists()){            return flag;         }        FileInputStream fis=null;        FileOutputStream fos=null;        try {            fis = new FileInputStream(sourcePath);            fos = new FileOutputStream(destinationPath);            byte buf[] = new byte[1024];            while(fis.read(buf)!=-1){                fos.write(buf);            }            flag=true;        } catch (Exception e) {            flag=false;            e.printStackTrace();        }finally{            try {                if(fis!=null){                fis.close();                }                if(fos!=null){                fos.close();                }            } catch (IOException e) {                flag=false;                e.printStackTrace();            }        }        return flag;    }    /**     * 删除文件:非文件夹     */    public static boolean deleteFile(String filePath){        boolean flag=false;        File file=new File(filePath);        if(file.exists() && !file.isDirectory()){            flag=file.delete();        }        return flag;    }    /**     * 移动文件:实现文件的移动及文件重命名     * @param sourcePath     * @param destinationPath     * @return     */    public  static boolean moveFile(String sourcePath,String destinationPath){        //若源文件路径和目标文件路径一致,则不操作,节省资源开销        if(sourcePath.equals(destinationPath)){            return true;        }        File file=new File(sourcePath);        if(!file.exists() && !file.isDirectory()){            return false;        }        return copyFile(sourcePath, destinationPath) && deleteFile(sourcePath);    }    /**     * 复制文件夹全部内容     * @param sourcePath     * @param destinationPath     * @return     */    public static boolean copyFileDir(String sourcePath,String destinationPath){        //路径一致则不复制,节省资源开销:实际项目很少复制同一个文件夹下复制,造成windows下的文件重命令的效果        if(sourcePath.equals(destinationPath)){            return true;        }        boolean flag=true;        File sourceDir=new File(sourcePath);        File destinationDir=new File(destinationPath);        //文件源文件不存在则终止        if(!sourceDir.exists()){            return false;        }        //目标文件不存在则创建        if(!destinationDir.exists()){             flag=flag && destinationDir.mkdirs();        }         File[] tempFileList = sourceDir.listFiles();         File tempFile = null;         String tempSourcePath="";         String tempDestinationPath="";         for (int i = 0; i < tempFileList.length; i++) {             tempFile=tempFileList[i];             if(tempFile.isFile()){                 tempSourcePath=sourcePath+File.separator+tempFile.getName();                 tempDestinationPath=destinationPath+File.separator+tempFile.getName();                 flag=flag && copyFile(tempSourcePath, tempDestinationPath);             }             //若目标文件为空,则创建空的文件夹             if(tempFile.isDirectory() && tempFile.listFiles().length==0){                new File(destinationPath+File.separator+tempFile.getName()).mkdirs();             }             //若目标文件非空,则迭代复制             if(tempFile.isDirectory() && tempFile.listFiles().length!=0){                //如果整个流程哪一方出现故障,则返回的结果是false                 flag=flag && copyFileDir(sourcePath+File.separator+tempFile.getName(), destinationPath+File.separator+tempFile.getName());             }           }        return flag;    }    /**     * 删除文件夹     * 难点:先删除文件夹包含文件,然后再删除文件夹     * @param dirPath     * @return     */    public static boolean delFileDir(String fileDirPath){            boolean flag=true;            File fileDir=new File(fileDirPath);            //判断目标文件是否存在和是否为文件夹,如果是空文件夹则直接删除返回            if(!fileDir.exists() || !fileDir.isDirectory()){                return false;            }else if(fileDir.list().length==0){                return fileDir.delete();            }             //获取文件夹下的文件名             String[] tempFileList = fileDir.list();             File tempFile = null;             String tempFilePath="";             for (int i = 0; i < tempFileList.length; i++) {                  //拼装临时文件路径                  if (fileDirPath.endsWith(File.separator)) {                      tempFilePath=fileDirPath+tempFileList[i];                  } else {                      tempFilePath=fileDirPath + File.separator + tempFileList[i];                  }                  tempFile=new File(tempFilePath);                   if(tempFile.isFile() ||(tempFile.isDirectory() && tempFile.listFiles().length==0)){                      flag=flag && tempFile.delete();                  }else{                      flag=flag && delFileDir(tempFilePath);                  }               }            //最后删除根文件路径            tempFile=new File(fileDirPath);            flag=flag && tempFile.delete();            return flag;    }    /**     * 移动文件夹     * @param sourceFileDirPath     * @param destinationFileDirPath     * @return     */    public static boolean moveFileDir(String sourceFileDirPath,String destinationFileDirPath){        boolean flag=true;        if(sourceFileDirPath.equals(destinationFileDirPath)){            return true;        }        File file=new File(sourceFileDirPath);        if(!file.isDirectory()){            return false;        }        flag=flag && copyFileDir(sourceFileDirPath, destinationFileDirPath);        flag=flag && delFileDir(sourceFileDirPath);        return flag;    }}
0 0
原创粉丝点击