FileTempIndexUtil.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.zx.dataservice.utils;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.*;
  9. public class FileTempIndexUtil {
  10. private static Set<String> fileNameSet = new HashSet<>();
  11. private static final Logger log = LoggerFactory.getLogger(FileTempIndexUtil.class);
  12. /**
  13. * 根据时间对文件进行排序
  14. * @param path
  15. * @return
  16. */
  17. public static File getFileSort(String path) {
  18. List<File> list = getFileList(path, new ArrayList<File>());
  19. if(0 == list.size()){
  20. return null;
  21. }
  22. // if (list != null && list.size() > 0) {
  23. // Collections.sort(list, new Comparator<File>() {
  24. // public int compare(File file, File newFile) {
  25. // if (file.lastModified() < newFile.lastModified()) {
  26. // return -1;
  27. // } else if (file.lastModified() == newFile.lastModified()) {
  28. // return 0;
  29. // } else {
  30. // return 1;
  31. // }
  32. // }
  33. // });
  34. // }
  35. return list.get(0);
  36. }
  37. public static List<File> getFile(String path) {
  38. List<File> list = getFileList(path, new ArrayList<File>());
  39. if(0 == list.size()){
  40. return null;
  41. }
  42. if (list != null && list.size() > 0) {
  43. Collections.sort(list, new Comparator<File>() {
  44. public int compare(File file, File newFile) {
  45. if (file.lastModified() < newFile.lastModified()) {
  46. return -1;
  47. } else if (file.lastModified() == newFile.lastModified()) {
  48. return 0;
  49. } else {
  50. return 1;
  51. }
  52. }
  53. });
  54. }
  55. return list;
  56. }
  57. /**
  58. * 获取文件
  59. * @param realpath
  60. * @param files
  61. * @return
  62. */
  63. public static List<File> getFileList(String realpath, List<File> files) {
  64. File realFile = new File(realpath);
  65. if (realFile.isDirectory()) {
  66. File[] subfiles = realFile.listFiles();
  67. for (File file : subfiles) {
  68. if (!file.isDirectory()) {
  69. if(fileNameSet.add(file.getName())) {
  70. files.add(file);
  71. if(10000 < fileNameSet.size()){
  72. fileNameSet = new HashSet<>();
  73. }
  74. return files;
  75. }
  76. }
  77. }
  78. }
  79. return files;
  80. }
  81. /**
  82. * 移动文件
  83. * @param sourceFileUrl 来源文件
  84. * @param goalFileUrl 目标文件夹
  85. */
  86. public static void moveFile(String sourceFileUrl, String goalFileUrl){
  87. // 源文件路径
  88. File startFile=new File(sourceFileUrl);
  89. // 目的目录路径
  90. File endDirection=new File(goalFileUrl);
  91. // 如果目的目录路径不存在,则进行创建
  92. if(!endDirection.exists()) {
  93. endDirection.mkdirs();
  94. }
  95. //目的文件路径=目的目录路径+源文件名称
  96. File endFile = new File(endDirection + File.separator + startFile.getName());
  97. try {
  98. // 调用File类的核心方法renameTo
  99. startFile.renameTo(endFile);
  100. }catch(Exception e) {
  101. log.error("文件移动出现异常!起始路径:{"+startFile.getAbsolutePath()+"}");
  102. }
  103. }
  104. public static String readFileContent(String fileName) {
  105. File file = new File(fileName);
  106. if(null == file||(!file.isFile())||(!file.exists())){
  107. return null;
  108. }
  109. BufferedReader reader = null;
  110. StringBuffer sbf = new StringBuffer();
  111. try {
  112. reader = new BufferedReader(new FileReader(file));
  113. String tempStr;
  114. while ((tempStr = reader.readLine()) != null) {
  115. sbf.append(tempStr);
  116. }
  117. reader.close();
  118. deleteFile(fileName);
  119. return sbf.toString();
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. } finally {
  123. if (reader != null) {
  124. try {
  125. reader.close();
  126. } catch (IOException e1) {
  127. e1.printStackTrace();
  128. }
  129. }
  130. }
  131. deleteFile(fileName);
  132. return sbf.toString();
  133. }
  134. public static boolean deleteFile(String path) {
  135. File file = new File(path);
  136. if (file.isFile() && file.exists()) {
  137. file.delete();
  138. return true;
  139. }
  140. return false;
  141. }
  142. }