zoukankan      html  css  js  c++  java
  • FileUtil

    public class FileUtil {

    /**
    * 复制文件到制定目录,覆盖目标文件
    * @Title: copyFileOverwrite
    * @Description:
    * @param srcPath
    * @param fileName
    * @param destPath
    * @throws IOException
    * @return void
    */

    public static void copyFileOverwrite(String srcFilePath, String destPath) throws IOException {
    File srcFile = new File(srcFilePath);
    if(srcFile.isDirectory() || !srcFile.exists())
    return;
    String fileName = srcFile.getName();
    File targetFile = new File(destPath + File.separator + fileName);
    if (targetFile.exists())
    targetFile.delete();
    FileUtil.copyFile(srcFilePath, destPath);
    }
    /**
    * 移动文件到制定目录,覆盖目标文件
    * @Title: copyFileOverwrite
    * @Description:
    * @param srcPath
    * @param fileName
    * @param destPath
    * @throws IOException
    * @return void
    */
    public static void moveFileOverwrite(String srcFilePath, String destPath) throws IOException {
    File srcFile = new File(srcFilePath);
    if(srcFile.isDirectory() || !srcFile.exists())
    return;
    String fileName = srcFile.getName();
    File targetFile = new File(destPath + File.separator + fileName);
    if (targetFile.exists())
    targetFile.delete();
    FileUtil.moveFile(srcFilePath, destPath);
    }

    /**
    * 复制文件或者目录
    *
    * @Title: copyFile
    * @Description: 复制文件或者目录
    * @param srcFilePath
    * 源文件路径
    * @param destFolder
    * 目标文件夹
    * @return void
    * @throws IOException
    */
    public static void copyFile(String srcFilePath, String destFolder) throws IOException {
    File srcFile = new File(srcFilePath);
    File destFile = new File(destFolder);
    if (srcFile.isDirectory()) {
    FileUtils.copyDirectoryToDirectory(srcFile, destFile);
    } else if (srcFile.isFile()) {
    FileUtils.copyFileToDirectory(srcFile, destFile, true);
    }
    }

    /**
    * 复制文件或者目录
    *
    * @Title: copyFile
    * @Description: 复制文件或者目录
    * @param srcFile
    * 源文件
    * @param destFile
    * 目标文件夹
    * @return void
    * @throws IOException
    */
    public static void copyFile(File srcFile, File destFile) throws IOException {
    if (srcFile.isDirectory()) {
    FileUtils.copyDirectoryToDirectory(srcFile, destFile);
    } else if (srcFile.isFile()) {
    FileUtils.copyFileToDirectory(srcFile, destFile, true);
    }
    }

    /**
    * 复制输入流到目标文件
    *
    * @Title: copyFile
    * @Description: 复制输入流到目标文件
    * @param inputStream
    * 输入流
    * @param destFile
    * 目标文件
    * @return void
    * @throws IOException
    */
    public static void copyFile(InputStream inputStream, File destFile) throws IOException {
    FileUtils.copyInputStreamToFile(inputStream, destFile);
    }
    /**
    *
    * @Title: fileUpload
    * @Description: 文件上传
    * @param rule 文件
    * @param request
    * @param response
    * @param uploadDir 上传路径
    * @throws IOException
    * @return void
    */
    @SuppressWarnings("deprecation")
    public static void fileUpload(MultipartFile rule,HttpServletRequest request,HttpServletResponse response, String uploadDir) throws IOException {
    response.setContentType("application/json; charset=UTF-8");
    //文件存储
    if(rule != null){
    if(null != rule.getOriginalFilename() && !"".equals(rule.getOriginalFilename())){
    String realpath = "" ;//获取服务器路径
    Properties properties = new Properties();
    properties =PropertiesLoaderUtils.loadAllProperties("app.properties");
    realpath = properties.getProperty("filePath");
    String fileName = rule.getOriginalFilename();
    FileUtil.copyFile(rule.getInputStream(), new File(realpath+"/"+uploadDir+"/"+fileName));
    }
    }
    }

    /**
    *
    * @Title: readFile
    * @Description: 读取文件
    * @param request
    * @param fileName
    * @param uploadDir 每个文件上传路径
    * @return
    * @throws IOException
    * @return String
    */
    @SuppressWarnings("deprecation")
    public static String readFile(HttpServletRequest request,String uploadDir) throws IOException{
    // String realpath = request.getRealPath("") ;//获取服务器路径
    String realpath = "" ;//获取服务器路径
    Properties properties = new Properties();
    properties =PropertiesLoaderUtils.loadAllProperties("app.properties");
    realpath = properties.getProperty("filePath");
    String fileName = null;
    //读取文件
    String[] file = FileUtil.listFilesbySuffix(realpath+"/"+uploadDir, "");
    if(null != file && file.length > 0){
    fileName = file[0];
    }
    return fileName;
    }

    /**
    *
    * @Title: deleteFile
    * @Description: 文件删除
    * @param rule
    * @param request
    * @param uploadDir 路径
    * @throws IOException
    * @return void
    */
    @SuppressWarnings("deprecation")
    public static void deleteFile(MultipartFile rule,HttpServletRequest request,String uploadDir) throws IOException{
    if(rule != null){
    if(null != rule.getOriginalFilename() && !"".equals(rule.getOriginalFilename())){
    // String realpath = request.getRealPath("") ;//获取服务器路径
    String realpath = "" ;//获取服务器路径
    Properties properties = new Properties();
    properties =PropertiesLoaderUtils.loadAllProperties("app.properties");
    realpath = properties.getProperty("filePath");
    File file = new File(realpath+"/"+uploadDir);
    if (file.exists()){
    File[] files = file.listFiles();//声明目录下所有的文件 files[];
    for (int i = 0; i < files.length; i++) {//遍历目录下所有的文件
    files[i].delete();
    }
    }
    }
    }
    }


    /**
    * 删除一个文件或者目录
    *
    * @Title: deleteFile
    * @Description: 删除一个文件或者目录
    * @param targetPath
    * 目标文件或者目录
    * @return void
    * @throws IOException
    */
    public static void deleteFile(String targetPath) throws IOException {
    File targetFile = new File(targetPath);
    if (!targetFile.exists())
    return;
    if (targetFile.isDirectory()) {
    FileUtils.deleteDirectory(targetFile);
    } else if (targetFile.isFile()) {
    targetFile.delete();
    }
    }

    /**
    * 删除一个文件或者目录
    *
    * @Title: deleteFile
    * @Description: 删除一个文件或者目录
    * @param targetFile
    * 目标文件或者目录
    * @return void
    * @throws IOException
    */
    public static void deleteFile(File targetFile) throws IOException {
    if (targetFile.isDirectory()) {
    FileUtils.deleteDirectory(targetFile);
    } else if (targetFile.isFile()) {
    targetFile.delete();
    }
    }

    /**
    * 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建
    *
    * @Title: moveFile
    * @Description: 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建
    * @param srcFilePath
    * 源文件或者目录路径
    * @param destFolder
    * 目标文件夹
    * @return void
    * @throws IOException
    */
    public static void moveFile(String srcFilePath, String destFolder) throws IOException {
    File srcFile = new File(srcFilePath);
    File destFile = new File(destFolder);
    if (srcFile.isDirectory()) {
    FileUtils.moveDirectoryToDirectory(srcFile, destFile, true);
    } else if (srcFile.isFile()) {
    FileUtils.moveFileToDirectory(srcFile, destFile, true);
    }
    }

    /**
    * 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建
    *
    * @Title: moveFile
    * @Description: 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建
    * @param srcFile
    * 源文件或者目录
    * @param destFile
    * 目标文件夹
    * @return void
    * @throws IOException
    */
    public static void moveFile(File srcFile, File destFile) throws IOException {
    if (srcFile.isDirectory()) {
    FileUtils.moveDirectoryToDirectory(srcFile, destFile, true);
    } else if (srcFile.isFile()) {
    FileUtils.moveFileToDirectory(srcFile, destFile, true);
    }
    }

    /**
    * 重命名文件或文件夹
    *
    * @Title: renameFile
    * @Description: 重命名文件或文件夹
    * @param srcFilePath
    * 源文件路径
    * @param newFileName
    * 重命名
    * @return boolean 操作成功标识
    */
    public static boolean renameFile(String srcFilePath, String newFileName) {
    String newFilePath = StringUtil.formatPath(getParentPath(srcFilePath) + "/" + newFileName);
    File srcFile = new File(srcFilePath);
    File newFile = new File(newFilePath);
    return srcFile.renameTo(newFile);
    }

    /**
    * 重命名文件或文件夹
    *
    * @Title: renameFile
    * @Description: 重命名文件或文件夹
    * @param srcFile
    * 源文件
    * @param newFileName
    * 重命名
    * @return boolean 操作成功标识
    */
    public static boolean renameFile(File srcFile, String newFileName) {
    String newFilePath = StringUtil.formatPath(srcFile.getParent() + "/" + newFileName);
    File newFile = new File(newFilePath);
    return srcFile.renameTo(newFile);
    }

    /**
    * 读取文件或者目录的大小
    *
    * @Title: genFileSize
    * @Description: 读取文件或者目录的大小
    * @param destFilePath
    * 目标文件或者文件夹路径
    * @return long 文件或者目录的大小,如果获取失败,则返回-1
    */
    public static long genFileSize(String destFilePath) {
    File destFile = new File(destFilePath);
    if (destFile.isFile()) {
    return destFile.length();
    } else if (destFile.isDirectory()) {
    return FileUtils.sizeOfDirectory(destFile);
    }
    return -1L;
    }

    /**
    * 读取文件或者目录的大小
    *
    * @Title: genFileSize
    * @Description: 读取文件或者目录的大小
    * @param destFile
    * 目标文件或者文件夹
    * @return long 文件或者目录的大小,如果获取失败,则返回-1
    */
    public static long genFileSize(File destFile) {
    if (destFile.isFile()) {
    return destFile.length();
    } else if (destFile.isDirectory()) {
    return FileUtils.sizeOfDirectory(destFile);
    }
    return -1L;
    }

    /**
    * 判断一个文件是否存在
    *
    * @Title: isExist
    * @Description: 判断一个文件是否存在
    * @param filePath
    * 文件路径
    * @return boolean 存在返回true,否则返回false
    */
    public static boolean isExist(String filePath) {
    if (StringUtil.isEmptyString(filePath))
    return false;
    return new File(filePath).exists();
    }

    /*
    * 函数名:getFile
    * 作用:使用递归,输出指定文件夹内的所有文件
    * 参数:path:文件夹路径 deep:表示文件的层次深度,控制前置空格的个数
    * 前置空格缩进,显示文件层次结构
    */
    @SuppressWarnings({ "unused", "null" })
    private static List<String> getFileName(String path,int deep) throws IOException {
    // 获得指定文件对象
    File file = new File(path);
    // 获得该文件夹内的所有文件
    File[] array = file.listFiles();
    //文件名称
    List<String> nameList = null;
    for(int i=0;i<array.length;i++){
    if(array[i].isFile()){ //如果是文件
    for (int j = 0; j < deep; j++)//输出前置空格
    System.out.print(" ");
    // 只输出文件名字
    nameList.add(array[i].getName());
    System.out.println( array[i].getName());
    // 输出当前文件的完整路径
    // System.out.println("#####" + array[i]);
    // 同样输出当前文件的完整路径 大家可以去掉注释 测试一下
    // System.out.println(array[i].getPath());
    }
    else if(array[i].isDirectory()){ //如果是文件夹
    for (int j = 0; j < deep; j++)//输出前置空格
    System.out.print(" ");

    System.out.println( array[i].getName());
    nameList.add(array[i].getName());
    //System.out.println(array[i].getPath());
    //文件夹需要调用递归 ,深度+1
    getFileName(array[i].getPath(),deep+1);
    }
    }
    return nameList;
    }
    }

  • 相关阅读:
    蓝桥杯省赛模拟:村庄建设、郊外植树。
    Andrew 算法(构造凸包)
    Codeforces Round #635 (Div. 2)(A, B, C, D)
    级边凸包构造法(extreme edge)
    极点的凸包构造算法(extreme point)
    Codeforces Round #633 (Div. 2)(A, B, C)
    凸包---graham scan算法 + 例题P2742
    Element-ui中ElScrollBar组件滚动条的使用
    HTTP:Web的基础
    HTTP报文
  • 原文地址:https://www.cnblogs.com/h-wei/p/10245156.html
Copyright © 2011-2022 走看看