package utils; import com.mycim.framework.utils.lang.StringUtils; import com.mycim.framework.utils.lang.collections.CollectionUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; import java.util.*; public class HandleFileUtils { private static final String XLS_TYPE = "XLS"; private static final String XLSX_TYPE = "XLSX"; private static final String C_COLON = ":"; private static final String C_COMMA = ","; public static final String DOT = "."; /** * 获取目标路径下指定后缀文件,并过滤掉指定后缀 * * @param directory 指定文件夹 * @param postFix 需要获取的文件后缀 * @param excludePostFix 需要过滤的文件后缀 * @return */ public static List readDirectoryFileFullName(String directory, String postFix, String excludePostFix) { List fileFullNames = new ArrayList<>(); File file = new File(directory); if (file.isDirectory()) { File[] files = file.listFiles(); for (File file1 : files) { if (file1.isDirectory()) { fileFullNames.addAll(readDirectoryFileFullName(file1.getPath(), postFix, excludePostFix)); } else { String fileNme = StringUtils.upperCase(file1.getName()); if (StringUtils.isNotEmpty(excludePostFix) && StringUtils.endsWith(fileNme, StringUtils.upperCase(excludePostFix))) { continue; } if (StringUtils.endsWith(fileNme, StringUtils.upperCase(postFix))) { fileFullNames.add(file1.getPath()); } } } } return fileFullNames; } /** * 获取目标路径下指定后缀文件,并过滤掉指定后缀 * * @param directory 指定文件夹 * @param postFix 需要获取的文件后缀 * @param excludePostFix 需要过滤的文件后缀 * @return */ public static Map readDirectoryFileNameForMap(String directory, List postFix, List excludePostFix, long time) { Map fileFullNames = new HashMap<>(); File file = new File(directory); if (file.isDirectory()) { File[] files = file.listFiles(); for (File file1 : files) { if (file1.isDirectory()) { fileFullNames.putAll(readDirectoryFileNameForMap(file1.getPath(), postFix, excludePostFix, time)); } else { if (file1.lastModified() < time) continue; String fileNme = StringUtils.upperCase(file1.getName()); boolean excludeFlag = false; if (CollectionUtils.isNotEmpty(excludePostFix)) { for (String excludeFix : excludePostFix) { if (StringUtils.endsWith(fileNme, StringUtils.upperCase(excludeFix))) { excludeFlag = true; break; } } if (excludeFlag) { continue; } } String filePostFix = StringUtils.substringAfterLast(fileNme, DOT); if (postFix.contains(filePostFix)) { fileFullNames.put(file1.getName(), file1.getPath()); } } } } return fileFullNames; } /** * 解析 csv文件 * * @param fileFullPath * @return */ public static Map readCsvFile(String fileFullPath) { BufferedReader br = null; String line = null; StringBuffer buf = new StringBuffer(); String code = judgeTextCode(fileFullPath); Map result = new HashMap(); Map headData = new HashMap(); List> rowData = new ArrayList<>(); try { // 根据文件路径创建缓冲输入流 br = new BufferedReader(new InputStreamReader(new FileInputStream(fileFullPath), code)); // 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中 int i = 1; while ((line = br.readLine()) != null) { line = StringUtils.trim(line); if (StringUtils.isBlank(line)) { continue; } int index = StringUtils.indexOf(line, C_COLON); if (index > 0) { String key = StringUtils.upperCase(StringUtils.trim(StringUtils.substringBefore(line, C_COLON))); String value = StringUtils.trim(StringUtils.substringAfter(line, C_COLON)); if (StringUtils.indexOf(value, C_COMMA) > 0) { value = StringUtils.substringBefore(value, C_COMMA); } headData.put(key, value); } else { String[] value = StringUtils.splitPreserveAllTokens(line, C_COMMA); if (value != null && value.length > 1) { rowData.add(Arrays.asList(value)); } } } } catch (Exception e) { // throw new SystemIllegalArgumentException(Errors.create().content("Read csv file fail!").build()); } finally { // 关闭流 if (br != null) { try { br.close(); } catch (IOException e) { // logger.error(e); System.out.println(e); br = null; } } } result.put("head", headData); result.put("body", rowData); return result; } public static String readTxtFile(String fileFullPath) { BufferedReader br = null; String line; // String code = judgeTextCode(fileFullPath); String code = "UTF-8"; StringBuilder resultBuilder = new StringBuilder(); try { // 根据文件路径创建缓冲输入流 br = new BufferedReader(new InputStreamReader(new FileInputStream(fileFullPath), code)); while ((line = br.readLine()) != null) { resultBuilder.append(StringUtils.trim(line)); } } catch (Exception e) { // throw new SystemIllegalArgumentException(Errors.create().content("Read csv file fail!").build()); System.out.println(e); } finally { // 关闭流 if (br != null) { try { br.close(); } catch (IOException e) { // logger.error(e); System.out.println(e); br = null; } } } return resultBuilder.toString(); } private static String judgeTextCode(String strFilePath) { FileInputStream fis = null; String code = ""; try { fis = new FileInputStream(strFilePath); int a = fis.read(); int b = fis.read(); if (a == 0xFF && b == 0xFE) { code = "Unicode"; } else if (a == 0xFE && b == 0xFF) { code = "UTF-16BE"; } else if (a == 0xEF && b == 0xBB) { code = "UTF-8"; } else { code = "GBK"; } } catch (Exception e) { // throw new SystemIllegalArgumentException(Errors.create().content("Get file Code fail!").build()); } finally { try { if (fis != null) { fis.close(); } } catch (Exception e) { fis = null; } } return code; } /** * 读取Excel数据 * * @param fileFullPath 文件全路径 * @param sheetIndex 表索引,第一个表为0 * @return */ public static List> readExcelFile(String fileFullPath, int sheetIndex) throws IOException { try { String excelType = getExcelType(fileFullPath); if (XLSX_TYPE.equals(excelType)) { return initXLSX(getFile(fileFullPath), sheetIndex, excelType); } else if (XLS_TYPE.equals(excelType)) { return initXLS(getFile(fileFullPath), sheetIndex, excelType); } return null; } catch (Exception e) { // throw new SystemIllegalArgumentException(Errors.create().content("Read excel file fail!").build()); throw e; } } private static List> initXLS(FileInputStream fileInputStream, int sheetIndex, String excelType) throws IOException { Workbook workbook = getHssfWorkbook(fileInputStream); Sheet sheet = getHssfSheet(workbook, sheetIndex); int totalRows = setTotalRows(sheet); return getStringArrayList(sheet, excelType, totalRows); } private static List> initXLSX(FileInputStream fileInputStream, int sheetIndex, String excelType) throws IOException { // 获取Excel引用 Workbook workbook = getXssfWorkbook(fileInputStream); // 获取Excel引用的第几张表 Sheet sheet = getXssfSheet(workbook, sheetIndex); // 获取总行数 int totalRows = setTotalRows(sheet); return getStringArrayList(sheet, excelType, totalRows); } /** * String类型集合的List集合
* * @param sheet * @param excelType 类型 * @param totalRows * @return */ public static List> getStringArrayList(Sheet sheet, String excelType, int totalRows) { List> stringArrayList = new ArrayList<>(); for (int i = 0; i < totalRows; i++) { Row row = isRowNotNull(sheet, excelType, i); if (row != null) { int totalCells = setTotalCells(row); List strings = new ArrayList<>(); for (int j = 0; j < totalCells; j++) { strings.add(getCellStringValue(sheet, excelType, i, j)); } stringArrayList.add(strings); } } return stringArrayList; } /** * 获取当前读取Excel文件类型 * * @return XLS_TYPE or XLSX_TYPE */ private static String getExcelType(String filePath) { return StringUtils.endsWith(filePath, XLSX_TYPE) ? XLSX_TYPE : XLS_TYPE; } /** * 获取文件字节流 * * @param filePath 文件路径 * @return FileInputStream * @throws FileNotFoundException */ private static FileInputStream getFile(String filePath) throws FileNotFoundException { return new FileInputStream(new File(filePath)); } /** * 获取第i行第j列的单元格的值 除日期类型,其他类型全处理为文本,与表格内容一致 * * @param i 行号 * @param j 列号 * @return String 单元格中的数据 */ public static String getCellStringValue(Sheet sheet, String excelType, int i, int j) { String val = null; Cell cell = getCell(sheet, excelType, i, j); if (cell != null) { if (cell.getCellTypeEnum().equals(CellType.NUMERIC) && DateUtil.isCellDateFormatted(cell)) { val = new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()); } else { cell.setCellType(CellType.STRING); val = cell.toString().trim(); } } return val == null ? "" : val.trim(); } /** * 获取第i行数据 * * @param i 行号 * @return boolean ? true : false */ public static Row isRowNotNull(Sheet sheet, String excelType, int i) { return getRow(sheet, excelType, i); } /** * 获取Excel的行 * * @param i 行号 * @return HSSFRow or XSSFRow */ public static Row getRow(Sheet sheet, String excelType, int i) { return XLSX_TYPE.equals(excelType) ? getXSSFRow(sheet, i) : getHSSFRow(sheet, i); } /** * 获取Excel的列 * * @param i 行号 * @param j 列号 * @return HSSFCell or XSSFCell */ public static Cell getCell(Sheet sheet, String excelType, int i, int j) { return XLSX_TYPE.equals(excelType) ? getXSSFCell(sheet, i, j) : getHSSFCell(sheet, i, j); } /** * 设置总行数 * * @param sheet 表单引用 HSSFSheet or XSSFSheet */ private static int setTotalRows(Sheet sheet) { return sheet.getLastRowNum() + 1; } /** * 设置某行的总列数 * * @param row 行引用 HSSFRow or XSSFRow */ private static int setTotalCells(Row row) { return row.getLastCellNum(); } /** * XLSX类型的Excel引用 * * @param file 文件字节流 * @return XSSFWorkbook * @throws IOException */ private static XSSFWorkbook getXssfWorkbook(InputStream file) throws IOException { return new XSSFWorkbook(file); } /** * XLSX类型的Excel表单 * * @param sheetIndex 表单索引 * @return XSSFSheet */ private static XSSFSheet getXssfSheet(Workbook workbook, int sheetIndex) { return (XSSFSheet) workbook.getSheetAt(sheetIndex); } /** * XLSX类型的Excel表单中的行 * * @param rowNum 行号 * @return XSSFRow */ private static XSSFRow getXSSFRow(Sheet sheet, int rowNum) { return (XSSFRow) sheet.getRow(rowNum); } /** * XLSX类型的Excel表单中的列 * * @param rowNum 行号 * @param cellNum 列号 * @return XSSFCell */ private static XSSFCell getXSSFCell(Sheet sheet, int rowNum, int cellNum) { return getXSSFRow(sheet, rowNum).getCell(cellNum); } /** * XLS类型的Excel引用 * * @param file 文件字节流 * @return HSSFWorkbook * @throws IOException */ private static HSSFWorkbook getHssfWorkbook(InputStream file) throws IOException { return new HSSFWorkbook(file); } /** * XLS类型的Excel表单 * * @param sheetIndex 表单索引,第一个表为0 * @return HSSFSheet */ private static HSSFSheet getHssfSheet(Workbook workbook, int sheetIndex) { return (HSSFSheet) workbook.getSheetAt(sheetIndex); } /** * XLS类型的Excel表单中的行 * * @param rowNum 行号 * @return HSSFRow */ private static HSSFRow getHSSFRow(Sheet sheet, int rowNum) { return (HSSFRow) sheet.getRow(rowNum); } /** * XLS类型的Excel表单中的列 * * @param rowNum 行号 * @param cellNum 列号 * @return HSSFCell */ private static HSSFCell getHSSFCell(Sheet sheet, int rowNum, int cellNum) { return getHSSFRow(sheet, rowNum).getCell(cellNum); } /** * 移动指定文件夹下的文件 * * @param files 需要移动的文件全路径名 * @param targetPath 移动文件的目标主路径 */ public static void moveFiles(Collection files, String targetPath) { // logger.info(files); System.out.println(files); files.forEach(filePath -> { File file = new File(filePath); if (file != null && file.isFile()) { String target = targetPath + File.separator + StringUtils.substringAfterLast(filePath, File.separator); try { Files.move(Paths.get(filePath), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { // logger.error("moveFiles error:" + e); System.out.println("moveFiles error:" + e); } } }); } /** * 移动指定文件夹下的文件 * * @param files 需要移动的文件全路径名 * @param targetPath 移动文件的目标主路径 */ public static void copyFiles(Collection files, String targetPath) { // logger.info(files); files.forEach(filePath -> { File file = new File(filePath); if (file != null && file.isFile()) { String target = targetPath + File.separator + StringUtils.substringAfterLast(filePath, File.separator); try { Files.copy(Paths.get(filePath), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { // logger.error("copyFiles error:" + e); System.out.println("moveFiles error:" + e); } } }); } }