• 文件File工具类


    package com.panchan.tsmese.utils;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    
    import org.apache.commons.codec.digest.DigestUtils;
    import org.apache.commons.io.IOUtils;
    import org.apache.commons.lang3.StringUtils;
    
    import net.lingala.zip4j.core.ZipFile;
    import net.lingala.zip4j.exception.ZipException;
    import net.lingala.zip4j.model.ZipParameters;
    import net.lingala.zip4j.util.Zip4jConstants;
    
    /**
     * @Description 文件工具类
     * @version 1.0
     * @since JDK1.8
     * @author
     * @Created on 2018年11月2日
     */
    public class FileUtil {
        
        /**
         * 压缩文件 (压缩文件生成路径不可以与被压缩文件在同一路径)
         * @param path 生成的压缩文件存放路徑含名稱
         * @param pathName 需要压缩的文件或文件夹
         */
        public static void zip(String path, String pathName) throws ZipException{
            File pathNameFile = new File(pathName);
            ZipFile zipFile = new ZipFile(path);
            File file=zipFile.getFile();
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            
            ZipParameters para=new ZipParameters();
            //设置压缩级别,压缩方法默认
            para.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            if(pathNameFile.isDirectory()){
                File[] files = pathNameFile.listFiles();
                for (int i = 0; i < files.length; i++) {
                    zipFile.addFile(files[i], para);
                    files[i].delete();
                }
            }else{
                zipFile.addFile(pathNameFile, para);
            }
            pathNameFile.delete();
        }
        
        /**
         *  文件转byte md5加密
         * @param filePath
         * @return
         * @throws Exception
         */
        @SuppressWarnings("deprecation")
        public static String getMd5(String filePath) throws Exception {
            File file1 = new File(filePath);
            FileInputStream fis = new FileInputStream(file1);
            String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
            IOUtils.closeQuietly(fis);
            return md5;
        }
        
        /**
         * 创建文件
         * @param path 生成文件路径包含文件名
         * @param object 需要写入文件的内容
         * @throws IOException
         */
        public static void createFile(String path, String object) throws IOException {
            
            File checkFile = new File(path);
            FileWriter writer = null;
            try {
                // 二、检查目标文件是否存在,不存在则创建
                if (!checkFile.exists()) {
                    checkFile.createNewFile();// 创建目标文件
                }
                // 三、向目标文件中写入内容
                writer = new FileWriter(checkFile, true);
                writer.append(object);
                writer.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (null != writer)  writer.close();
            }
        }
        
        /**
         * 
         * @param path 文件路径
         * @param beginIndex 第几行开始
         * @param endIndex 末尾空余几行
         * @return
         */
        public static String readFile(String path, Integer beginIndex, Integer endIndex){
            File file = new File(path);
            InputStream inputStream = null;
            BufferedReader br = null;
            if(file.exists()){
                try {
                    inputStream = new FileInputStream(file);
                    br = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuffer buffer = new StringBuffer();
                    String str = "";
                    Integer begin = beginIndex != null ? beginIndex : 0;
                    Integer end = getFileLineNumber(file) - (endIndex != null ? endIndex : 0);
                    int i = 0;
                    while (!StringUtils.isEmpty(str = br.readLine())) {
                        i++;
                        if(i > begin && i <= end){
                            buffer.append(str);
                        }
                    }
                    return buffer.toString();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }catch(IOException e){
                    e.printStackTrace();
                }finally{
                    try {
                        if(inputStream != null){
                            inputStream.close();
                        }
                        if(br != null){
                            br.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }
        
        /**
         * 获取文件行数
         * @param file
         * @return
         * @throws IOException
         */
        public static int getFileLineNumber(File file) throws IOException {
                LineNumberReader lnr = new LineNumberReader(new FileReader(file));
                lnr.skip(Long.MAX_VALUE);
                int lineNo = lnr.getLineNumber() + 1;
                lnr.close();
                return lineNo;
        }
        
        /**
         * 读取Pem文件
         * @param path
         * @return
         */
        public static String readPemFile(String path) {
            BufferedReader br;
            String s = "";
            StringBuffer publickey = null;
            try {
                File file = new File(path);
                if(file.exists()){
                    br = new BufferedReader(new FileReader(path));
                    publickey =  new StringBuffer();
                    s = br.readLine();
                    while (s != null) {
                        if (s.charAt(0) != '-') {
                            publickey.append(s + "
    ");
                        }
                        s = br.readLine();
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return publickey == null ? "" : publickey.toString();
        }
    }
  • 相关阅读:
    关于html元素Css样式设置的一点心得(特别是与位置有关的,还有外边距、内边距有关的那些)
    【idea的一个安装细节】是不是使用idea不能连接网络了?
    html中a标签属性parent和self的举例说明
    关于jquery的each的操作;
    superagent中文文档
    mongoose 查询子文档的方法
    Object.prototype.toString.call()进行类型判断
    局部函数的直接引用与调用
    数据模型中某个字段是单选或者多选的定义方式;
    nodejs项目中的路由写法
  • 原文地址:https://www.cnblogs.com/huyanlon/p/10641355.html
Copyright © 2020-2023  润新知