• java FileUtils 文件工具类


    package com.sicdt.library.core.utils;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.zip.CRC32;
    import java.util.zip.CheckedOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    import org.apache.commons.io.IOUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    /**
     * 
     * <br>类 名: FileUtils 
     * <br>描 述: 描述类完成的主要功能 
     * <br>作 者: shizhenwei 
     * <br>创 建: 2017年5月15日 
     * <br>版 本: v0.0.2 
     * <br>
     * <br>历 史: (版本) 作者 时间 注释
     */
    public class FileUtils {
    
        private Logger log = LoggerFactory.getLogger(FileUtils.class);
        
        private File file;
        
        public FileUtils(File file) {
            this.file = file;
        }
        
        public FileUtils(String pathname) {
            this(new File(pathname));
        }
        
        public static FileUtils create(String pathname) {
            return new FileUtils(pathname);
        }
        
        public static FileUtils create(File file) {
            return new FileUtils(file);
        }
        
        /**
         * 获取不带扩展名的文件名
         * @return
         */
        public String getNameWithOutSuffix() {
            String name = file.getName();
            if(!file.isFile()) return name;
            int flag = name.lastIndexOf(".");
            if(flag != -1){
                return name.substring(0, flag);
            }
            return name;
        }
        
        /**
         * 获取文件扩展名
         * @return
         */
        public String getSuffix(){
            if(!file.isFile()) return "";
            String name = file.getName();
            int flag = name.lastIndexOf(".");
            if(flag != -1){
                return name.substring(flag + 1).toLowerCase();
            }
            return "";
        }
        
        /**
         * 获取当前文件所在文件夹路径
         * @return
         */
        public String getFolderPath() {
            File parent = file.getParentFile();
            if(parent == null || !parent.exists()){
                return File.separator;
            }
            return parent.getAbsolutePath();
        }
        
        /**
         * 将文件复制到
         * @param pathname 文件夹名称
         * @return
         */
        public File copyTo(String pathname) {
            File targetFile = new File(pathname);
            if(file.isDirectory()){
                if(!targetFile.exists() || !targetFile.isDirectory()){
                    targetFile.mkdirs();
                }
                log.info("Copy folder to: " + pathname);
                File[] childFiles = file.listFiles();
                if(childFiles != null && childFiles.length > 0){
                    for(File childFile: childFiles){
                        String targetpath = targetFile.getAbsolutePath() + File.separator + childFile.getName();
                        FileUtils childUtils = FileUtils.create(childFile);
                        childUtils.copyTo(targetpath);
                    }
                }
            }else{
                try(InputStream input = new FileInputStream(file);
                        OutputStream output = new FileOutputStream(targetFile);){
                    IOUtils.copy(input, output);
                    log.info("Copy file to: " + pathname);
                }catch(IOException e){
                    e.printStackTrace();
                    return null;
                }
            }
            return targetFile;
        }
        
        public boolean exist() {
            return file.exists();
        }
        
        /**
         * 压缩至文件夹
         * @param foldername
         * @return
         */
        public File zipTo(String foldername, String zipname) {
            if(StringUtils.isEmpty(zipname)){
                zipname = getNameWithOutSuffix() + ".zip";
            }
            File targetFolder = new File(foldername);
            if(!targetFolder.exists() || !targetFolder.isDirectory()){
                targetFolder.mkdirs();
            }
            File targetFile = new File(targetFolder.getAbsolutePath() + File.separator + zipname);
            try(FileOutputStream output = new FileOutputStream(targetFile);
                    CheckedOutputStream cos = new CheckedOutputStream(output, new CRC32());
                    ZipOutputStream zipout = new ZipOutputStream(cos);){
                compress(file, zipout, "");
                return targetFile;
            }catch(Exception e){
                log.error("文件压缩失败:" + targetFile.getAbsolutePath());
                return null;
            }
        }
        
        private void compress(File zipfile, ZipOutputStream out, String basedir) {
            if(zipfile.isDirectory()){
                File[] files = zipfile.listFiles();
                for(File childFile: files){
                    compress(childFile, out, basedir + zipfile.getName() + "/");
                }
            }else{
                try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipfile));){
                    ZipEntry entry = new ZipEntry(basedir + zipfile.getName());
                    out.putNextEntry(entry);
                    IOUtils.copy(bis, out);
                }catch(Exception e){
                    log.error("压缩失败:" + zipfile.getAbsolutePath() + ": " + e.getMessage(), e);
                }
            }
        }
        
        public void delete(){
            delete(file);
        }
        
        private boolean delete(File file) {
            if (file.isDirectory()) {
                String[] children = file.list();
                for (int i=0; i<children.length; i++) {
                    boolean success = delete(new File(file, children[i]));
                    if (!success) {
                        return false;
                    }
                }
            }
            return file.delete();
        }
        
        /**
         * 获取文件MD5
         * @return
         */
        public String md5() {
            return MD5Utils.getFileMD5String(file);
        }
    }
  • 相关阅读:
    NET Core入门笔记
    NET Core入门笔记
    NET Core入门笔记
    也许,这样理解HTTPS更容易
    也许,这样理解HTTPS更容易
    也许,这样理解HTTPS更容易
    10 个实战及面试常用 Shell 脚本编写
    10 个实战及面试常用 Shell 脚本编写
    10 个实战及面试常用 Shell 脚本编写
    7617:输出前k大的数
  • 原文地址:https://www.cnblogs.com/zwcry/p/8483322.html
Copyright © 2020-2023  润新知