前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java 调用 exe 可执行程序 并通过 文本文件交换数据

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

作者头像
用户10125653
发布2022-11-10 21:58:12
4550
发布2022-11-10 21:58:12
举报
文章被收录于专栏:changechange

导入 识别文件编码依赖

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.dat\n#Method\n1";

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

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

    /**
     * 数据写入文件
     * @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());
    }
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-04-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 导入 识别文件编码依赖
  • 编写文本工具类
  • 调用 exe 可执行文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com