java 调用 exe 可执行程序 并通过 文本文件交换数据

2022-11-10 21:58:12 浏览数 (1)

导入 识别文件编码依赖

compile group: 'com.googlecode.juniversalchardet', name: 'juniversalchardet', version: '1.0.3'

编写文本工具类

代码语言:javascript复制
@Slf4j
public class ExeInteractive {

    /**
     * bpa原始数据 地址
     */
    public final static String dataFile = "./data.dat";

    /**
     * 调用exe 配置文件 地址
     */
    public final static String config = "./config.txt";

    /**
     * 操作日志 地址
     */
    public final static String dataInfo = "./data_info.txt";

    /**
     * 读取合环后节点稳态潮流起始点
     */
    public final static Integer nodeStartIndex = 3;

    /**
     * 读取合环后稳态潮流结果起始点
     */
    public final static Integer resultStartIndex = 1;
    /**
     * 读取合环后稳态潮流结果结束点
     */
    public final static Integer resultEndIndex = 13;

    /**
     * 读取合环后90m内稳态电流起始点
     */
    public final static Integer trendStartIndex = 15;

    /**
     *读取合环后90m内稳态电流结束点
     */
    public final static Integer trendEndIndex = 103;

    /**
     *可执行文件 地址
     */
    public final static String CloseLoopCalDllExe = "./CloseLoopCalDllExe/CloseLoopCalEXE.exe";

    /**
     *开环个节点 支路 稳态潮流数据 地址
     */
    public final static String closeDataFile = "./data_res0.out";

    /**
     *闭环个节点 之路 稳态潮流数据 地址
     */
    public final static String openDataFile = "./data_res1.out";

    /**
     * 闭环稳态潮流结果 及 90m内稳态电流数据地址
     */
    public final static String trendDataFile = "./data_res2.out";

    /**
     * 优化分析后的bpa数据地址
     */
    public final static String optimalTrendDataFile = "./data_pso.out";

    /**
     * 优化分析后的 个节点及支路 稳态潮流数据 地址
     */
    public final static String optimalDataFile = "./data_pso.dat";

    /**
     * 调用exe合理性分析配置
     */
    public final static String reasonableConfig = "#BPA 文件路径n./data.datn#Methodn1";

    /**
     * 调用exe可信性分析配置
     */
    public final static String feasibleConfig = "#BPA 文件路径n./data.datn#Methodn2";

    /**
     * 调用exe优化分析配置
     */
    public final static String optimalConfig = "#BPA 文件路径n./data.datn#Methodn3";

    /**
     * 数据写入文件
     * @param data 字节数据
     * @param filePath 文件路径
     * @throws IOException 异常
     */
    public static void writeDataFile(byte[] data, String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            // 如果文件找不到,就new一个
            boolean newFile = file.createNewFile();
            if (newFile) {
                log.debug("潮流数据文件创建成功");
            }
        }
        // 定义输出流,写入文件的流
        OutputStream outputStream = new FileOutputStream(file);
        // 把string转换成byte型的,并存放在数组中
        outputStream.write(data);
        outputStream.close();
    }

    /**
     * 删除文件
     * @param filePathList 文件地址集合
     * @throws IOException 异常
     */
    public static void deleteDataFile(List<String> filePathList) throws IOException {
        filePathList.forEach(filePath -> {
            File file = new File(filePath);
            if (file.exists()) {
                boolean FileDelete = file.delete();
                if (FileDelete) {
                    log.debug("[{}],潮流数据缓存清除", file.getName());
                }

            }
        });

    }

    /**
     * 判断文件是否存在
     * @param filePath  文件路径
     * @return boolean
     */
    public static boolean existsDataFile(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }

    /**
     * 读取文件内容
     * @param filePath 文件路径
     * @return 文件字节数据
     * @throws IOException 异常
     */
    public static byte[] readDataFile(String filePath) throws IOException {
        File file = new File(filePath);
        InputStream inputStream = new FileInputStream(file);
        byte[] bytes = inputStream.readAllBytes();
        inputStream.close();
        return bytes;
    }


    /**
     * 读取文件内容
     * @return 文件字节数据
     */
    public static String readLogInfo()  {
        String dataInfo= "";
        try {
            byte[] bytes = readDataFile(ExeInteractive.dataInfo);
            dataInfo=new String(bytes,ObjectToBpa.CHARACTER_ENCODING);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataInfo;
    }

    /**
     * 判断文件字符集
     * @param bytes 字节数据
     * @return String
     */
    public static String getEncoding(byte[] bytes) {
        String DEFAULT_ENCODING = "UTF-8";
        UniversalDetector detector = new UniversalDetector(null);
        detector.handleData(bytes, 0, bytes.length);
        detector.dataEnd();
        String encoding = detector.getDetectedCharset();
        detector.reset();
        if (encoding == null) {
            encoding = DEFAULT_ENCODING;
        }
        return encoding;
    }


}

调用 exe 可执行文件

代码语言:javascript复制
 @Override
    public BranchVo updateByOptimizationExe(Long planId) {
        BpaBo bpaBo = writeBpa(planId);
        try {
            ExeInteractive.deleteDataFile(List.of(ExeInteractive.optimalDataFile, ExeInteractive.optimalTrendDataFile));
            ExeInteractive.writeDataFile(ExeInteractive.optimalConfig.getBytes(ObjectToBpa.CHARACTER_ENCODING), ExeInteractive.config);
            ExeInteractive.writeDataFile(bpaBo.getData().getBytes(ObjectToBpa.CHARACTER_ENCODING), ExeInteractive.dataFile);
            Runtime.getRuntime().exec(ExeInteractive.CloseLoopCalDllExe);
            Thread.sleep(calculateTime);
            if (ExeInteractive.existsDataFile(ExeInteractive.optimalTrendDataFile)) {
                BranchVo openData = this.getOpenData(ExeInteractive.optimalTrendDataFile, bpaBo);
                if (CollectionUtil.isNotEmpty(openData.getSafetyAnalysisNode())) {
                    this.update(Wrappers.<Plan>lambdaUpdate().eq(Plan::getId, planId).set(Plan::getOptimizeResult, OptimizeResult.SUCCESS.getValue()));
                    return openData;
                }
            }
        } catch (IOException | InterruptedException e) {
            throw new CommonException("调用可行性分析算法异常,请重新分析");
        }
        throw new CommonException(ExeInteractive.readLogInfo());
    }

0 人点赞