FileTempIndexUtil.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. if(40 == files.size()) {
  75. return files;
  76. }
  77. // return files;
  78. }
  79. }
  80. }
  81. }
  82. return files;
  83. }
  84. /**
  85. * 移动文件
  86. * @param sourceFileUrl 来源文件
  87. * @param goalFileUrl 目标文件夹
  88. */
  89. public static void moveFile(String sourceFileUrl, String goalFileUrl){
  90. // 源文件路径
  91. File startFile=new File(sourceFileUrl);
  92. // 目的目录路径
  93. File endDirection=new File(goalFileUrl);
  94. // 如果目的目录路径不存在,则进行创建
  95. if(!endDirection.exists()) {
  96. endDirection.mkdirs();
  97. }
  98. //目的文件路径=目的目录路径+源文件名称
  99. File endFile = new File(endDirection + File.separator + startFile.getName());
  100. try {
  101. // 调用File类的核心方法renameTo
  102. startFile.renameTo(endFile);
  103. }catch(Exception e) {
  104. log.error("文件移动出现异常!起始路径:{"+startFile.getAbsolutePath()+"}");
  105. }
  106. }
  107. public static String readFileContent(String fileName) {
  108. File file = new File(fileName);
  109. if(null == file||(!file.isFile())||(!file.exists())){
  110. return null;
  111. }
  112. BufferedReader reader = null;
  113. StringBuffer sbf = new StringBuffer();
  114. try {
  115. reader = new BufferedReader(new FileReader(file));
  116. String tempStr;
  117. while ((tempStr = reader.readLine()) != null) {
  118. sbf.append(tempStr);
  119. }
  120. reader.close();
  121. deleteFile(fileName);
  122. return sbf.toString();
  123. } catch (IOException e) {
  124. e.printStackTrace();
  125. } finally {
  126. if (reader != null) {
  127. try {
  128. reader.close();
  129. } catch (IOException e1) {
  130. e1.printStackTrace();
  131. }
  132. }
  133. }
  134. deleteFile(fileName);
  135. return sbf.toString();
  136. }
  137. public static boolean deleteFile(String path) {
  138. File file = new File(path);
  139. if (file.isFile() && file.exists()) {
  140. file.delete();
  141. return true;
  142. }
  143. return false;
  144. }
  145. }