| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package com.leanwo.management.util;
- import java.io.BufferedReader;
- import java.io.Closeable;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.leanwo.management.model.ApplicationSetting;
- /**
- * 获取程序上级文件夹下的所有文件下的stop.bat和run.bat
- * @author GuoZhiBo
- *
- */
- public class FileUtil {
- private final static Logger logger = LoggerFactory.getLogger(FileUtil.class);
- /**
- * 获取文件stop.bat和run.bat
- * @param closeables
- */
- public List<ApplicationSetting> getFile() {
- List<ApplicationSetting> settings = new ArrayList<>();
- File directory = new File("");
- String path = directory.getAbsolutePath();
- File parentPath = new File(path).getParentFile();
- // 取 文件/文件夹
- File files[] = parentPath.listFiles();
-
- // 对象为空 直接返回
- if(files == null){
- return settings;
- }
- // 存在文件 遍历 判断
- for (File f : files) {
- // 判断是否为 文件夹
- if(f.isDirectory()){
- ApplicationSetting applicationSetting = new ApplicationSetting();
- String filePath = f.getAbsolutePath();
- if(path.equals(filePath)) {
- continue;
- }
- File runFile = new File(filePath + "\\run.bat");
- if(!runFile.exists()) {
- continue;
- }
- File stopFile = new File(filePath + "\\stop.bat");
- if(!stopFile.exists()) {
- continue;
- }
- String name = readFileContent(runFile);
- name = name.replace("::", "").replace(" ", "").replace("echo", "").replace("title", "");
- if(name == null || name.length() <= 0) {
- name = f.getName();
- }
- applicationSetting.setName(name);
- applicationSetting.setInstallPath(filePath);
- applicationSetting.setStartBatFile("run.bat");
- applicationSetting.setStopBatFile("stop.bat");
- settings.add(applicationSetting);
- }
-
- }
- return settings;
- }
-
- public static String readFileContent(File file) {
- BufferedReader reader = null;
- StringBuffer sbf = new StringBuffer();
- try {
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
- String tempStr = reader.readLine();
- sbf.append(tempStr);
- // while ((tempStr = reader.readLine()) != null) {
- // sbf.append(tempStr);
- // }
- reader.close();
- return sbf.toString();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- return sbf.toString();
- }
- }
|