123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- 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 FileTempIndexUtil {
- private static Set<String> fileNameSet = new HashSet<>();
- private static final Logger log = LoggerFactory.getLogger(FileTempIndexUtil.class);
- /**
- * 根据时间对文件进行排序
- * @param path
- * @return
- */
- public static File getFileSort(String path) {
- List<File> list = getFileList(path, new ArrayList<File>());
- if(0 == list.size()){
- return null;
- }
- // if (list != null && list.size() > 0) {
- // Collections.sort(list, new Comparator<File>() {
- // 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<File> getFile(String path) {
- List<File> list = getFileList(path, new ArrayList<File>());
- if(0 == list.size()){
- return null;
- }
- if (list != null && list.size() > 0) {
- Collections.sort(list, new Comparator<File>() {
- 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<File> getFileList(String realpath, List<File> 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;
- }
- }
- }
- }
- 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||(!file.isFile())||(!file.exists())){
- 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;
- }
- }
|