package com.zx.dataservice.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.*; public class FileStockUtil { private static Set fileNameSet = new HashSet<>(); private static final Logger log = LoggerFactory.getLogger(FileStockUtil.class); /** * 根据时间对文件进行排序 * @param path * @return */ public static File getFileSort(String path) { List list = getFileList(path, new ArrayList()); if(0 == list.size()){ return null; } // if (list != null && list.size() > 0) { // Collections.sort(list, new Comparator() { // public int compare(File file, File newFile) { // if (file.lastModified() < newFile.lastModified()) { // return -1; // } else if (file.lastModified() == newFile.lastModified()) { // return 0; // } else { // return 1; // } // } // }); // } return list.get(0); } public static List getFile(String path) { List list = getFileList(path, new ArrayList()); if(0 == list.size()){ return null; } if (list != null && list.size() > 0) { Collections.sort(list, new Comparator() { public int compare(File file, File newFile) { if (file.lastModified() < newFile.lastModified()) { return -1; } else if (file.lastModified() == newFile.lastModified()) { return 0; } else { return 1; } } }); } return list; } /** * 获取文件 * @param realpath * @param files * @return */ public static List getFileList(String realpath, List files) { File realFile = new File(realpath); if (realFile.isDirectory()) { File[] subfiles = realFile.listFiles(); for (File file : subfiles) { if (!file.isDirectory()) { if(fileNameSet.add(file.getName())) { files.add(file); if(10000 < fileNameSet.size()){ fileNameSet = new HashSet<>(); } // return files; if(10 == files.size()) { return files; } } } } } return files; } /** * 移动文件 * @param sourceFileUrl 来源文件 * @param goalFileUrl 目标文件夹 */ public static void moveFile(String sourceFileUrl, String goalFileUrl){ // 源文件路径 File startFile=new File(sourceFileUrl); // 目的目录路径 File endDirection=new File(goalFileUrl); // 如果目的目录路径不存在,则进行创建 if(!endDirection.exists()) { endDirection.mkdirs(); } //目的文件路径=目的目录路径+源文件名称 File endFile = new File(endDirection + File.separator + startFile.getName()); try { // 调用File类的核心方法renameTo startFile.renameTo(endFile); }catch(Exception e) { log.error("文件移动出现异常!起始路径:{"+startFile.getAbsolutePath()+"}"); } } public static String readFileContent(String fileName) { File file = new File(fileName); if(null == file){ return null; } BufferedReader reader = null; StringBuffer sbf = new StringBuffer(); try { reader = new BufferedReader(new FileReader(file)); String tempStr; while ((tempStr = reader.readLine()) != null) { sbf.append(tempStr); } reader.close(); // deleteFile(fileName); return sbf.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } // deleteFile(fileName); return sbf.toString(); } public static boolean deleteFile(String path) { File file = new File(path); if (file.isFile() && file.exists()) { file.delete(); return true; } return false; } }