FileUtil.java 3.0 KB

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