whyclxw
2024-10-16 df04d2f5d56cce23aa108c1cce1ce27e4bf52fa0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.whyc.util;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
public class AnalysisUtil {
    /*String voltagePredictExePath = "D:\\10小时率电压预估\\voltage_predict_process\\main.exe";
    String excelSourcePath = "D:\\10小时率电压预估\\测试数据.xlsx";
    String excelOutPutPath = "D:\\10小时率电压预估\\输出目录";*/
    public  static String voltagePredictExePath="E:\\GitWorkSpace\\fg_v2.0\\target\\10小时率电压预估\\voltage_predict_process\\main.exe";
    public  static String excelOutPutPath="E:\\GitWorkSpace\\fg_v2.0\\target\\outPath";
    public  static String excelSourcePath="E:\\GitWorkSpace\\fg_v2.0\\target\\anaysis\\测试数据.xlsx";
 
    public static void analysisXlx(String excelSourcePath,String voltagePredictExePath,String excelOutPutPath){
        Process mProcess;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        try {
            // 启动进程,进行数据预估
            List<String> command = new ArrayList<>();
            command.add(voltagePredictExePath); // 电压趋势预测执行文件路径
            command.add(String.format("--read=\"%s\"", excelSourcePath)); // 源文件路径
            command.add(String.format("--write=\"%s\"", excelOutPutPath)); // 输出文件夹
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            mProcess = processBuilder.start();
            if (mProcess == null) {
                System.out.println("预测主进程启动失败");
                return;
            }
 
            successResult = new BufferedReader(new InputStreamReader(mProcess.getInputStream(), "GBK"));
            errorResult = new BufferedReader(new InputStreamReader(mProcess.getErrorStream(), "GBK"));
            String s;
            while ((s = successResult.readLine()) != null) {
                if ((s.length() > 0) && !"\n".equals(s)) {
                    System.out.println(String.format("SuccessMsg:%s", s));
                }
            }
            while ((s = errorResult.readLine()) != null) {
                if ((s.length() > 0) && !"\n".equals(s)) {
                    System.out.println(String.format("ErrorMsg:%s", s));
                }
            }
 
            if (mProcess != null) {
                mProcess.waitFor();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            safeClose(errorResult);
            safeClose(successResult);
        }
    }
    /**
     * 安全的关闭资源
     *
     * @param autoCloseable 资源
     */
    public static void safeClose(AutoCloseable autoCloseable) {
        if (autoCloseable == null)
            return;
 
        try {
            autoCloseable.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
 
}