FileUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.leanwo.management.util;
  2. import java.io.BufferedReader;
  3. import java.io.Closeable;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import com.leanwo.management.model.ApplicationSetting;
  14. /**
  15. * 获取程序上级文件夹下的所有文件下的stop.bat和run.bat
  16. * @author GuoZhiBo
  17. *
  18. */
  19. public class FileUtil {
  20. private final static Logger logger = LoggerFactory.getLogger(FileUtil.class);
  21. /**
  22. * 获取文件stop.bat和run.bat
  23. * @param closeables
  24. */
  25. public List<ApplicationSetting> getFile() {
  26. List<ApplicationSetting> settings = new ArrayList<>();
  27. File directory = new File("");
  28. String path = directory.getAbsolutePath();
  29. File parentPath = new File(path).getParentFile();
  30. // 取 文件/文件夹
  31. File files[] = parentPath.listFiles();
  32. // 对象为空 直接返回
  33. if(files == null){
  34. return settings;
  35. }
  36. // 存在文件 遍历 判断
  37. for (File f : files) {
  38. // 判断是否为 文件夹
  39. if(f.isDirectory()){
  40. ApplicationSetting applicationSetting = new ApplicationSetting();
  41. String filePath = f.getAbsolutePath();
  42. if(path.equals(filePath)) {
  43. continue;
  44. }
  45. File runFile = new File(filePath + "\\run.bat");
  46. if(!runFile.exists()) {
  47. continue;
  48. }
  49. File stopFile = new File(filePath + "\\stop.bat");
  50. if(!stopFile.exists()) {
  51. continue;
  52. }
  53. String name = readFileContent(runFile);
  54. name = name.replace("::", "").replace(" ", "").replace("echo", "").replace("title", "");
  55. if(name == null || name.length() <= 0) {
  56. name = f.getName();
  57. }
  58. applicationSetting.setName(name);
  59. applicationSetting.setInstallPath(filePath);
  60. applicationSetting.setStartBatFile("run.bat");
  61. applicationSetting.setStopBatFile("stop.bat");
  62. settings.add(applicationSetting);
  63. }
  64. }
  65. return settings;
  66. }
  67. public static String readFileContent(File file) {
  68. BufferedReader reader = null;
  69. StringBuffer sbf = new StringBuffer();
  70. try {
  71. reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
  72. String tempStr = reader.readLine();
  73. sbf.append(tempStr);
  74. // while ((tempStr = reader.readLine()) != null) {
  75. // sbf.append(tempStr);
  76. // }
  77. reader.close();
  78. return sbf.toString();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. } finally {
  82. if (reader != null) {
  83. try {
  84. reader.close();
  85. } catch (IOException e1) {
  86. e1.printStackTrace();
  87. }
  88. }
  89. }
  90. return sbf.toString();
  91. }
  92. }