1. 程式人生 > >文件File工具類

文件File工具類

ace com dig check ges clas ear 壓縮 core

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 + "\r"); } s = br.readLine(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return publickey == null ? "" : publickey.toString(); } }

文件File工具類