Java调用python的核心代码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//



import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class PPGPreprocessor {
    String exe_path = null;

    public PPGPreprocessor(String exe_path) {
        this.exe_path = exe_path;
    }

    public Wave preprocess(String file_path) {
        Process pr = null;

        try {
            pr = Runtime.getRuntime().exec(this.exe_path + " --file_path " + file_path);
        } catch (IOException var12) {
            throw new InvalidWaveException("Running exe error", -1);
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = null;
        ArrayList<Double> rawDCDataPoints = new ArrayList();
        ArrayList<Integer> startIndices = new ArrayList();
        ArrayList endIndices = new ArrayList();

        try {
            line = in.readLine();
            if (line.startsWith("Error")) {
                if (line.endsWith("0")) {
                    throw new InvalidWaveException("Reading data error", 0);
                }

                if (line.endsWith("1")) {
                    throw new InvalidWaveException("Wave not long enough", 1);
                }

                if (line.endsWith("2")) {
                    throw new InvalidWaveException("No valid wavelets detected", 2);
                }

                if (line.endsWith("3")) {
                    throw new InvalidWaveException("No valid waveform detected", 3);
                }
            }

            String[] var11;
            int var10 = (var11 = line.split(",")).length;

            String idx;
            int var9;
            for(var9 = 0; var9 < var10; ++var9) {
                idx = var11[var9];
                rawDCDataPoints.add(Double.parseDouble(idx.trim()));
            }

            line = in.readLine();
            var10 = (var11 = line.split(",")).length;

            for(var9 = 0; var9 < var10; ++var9) {
                idx = var11[var9];
                startIndices.add(Integer.parseInt(idx.trim()));
            }

            line = in.readLine();
            var10 = (var11 = line.split(",")).length;

            for(var9 = 0; var9 < var10; ++var9) {
                idx = var11[var9];
                endIndices.add(Integer.parseInt(idx.trim()));
            }

            in.close();
            pr.destroy();
            return new Wave(rawDCDataPoints, startIndices, endIndices);
        } catch (IOException var13) {
            throw new InvalidWaveException("Running exe error", -1);
        }
    }

    public Wave preprocess(double[] data) {
        String file_path = (new File(this.exe_path)).getParentFile().getAbsolutePath();
        File tempFile = null;

        FileWriter f;
        try {
            new File(file_path);
            tempFile = File.createTempFile("tempPPGData", ".txt");
            f = new FileWriter(tempFile);
            double[] var9 = data;
            int var8 = data.length;

            for(int var7 = 0; var7 < var8; ++var7) {
                double x = var9[var7];
                f.write(String.valueOf(x) + " ");
            }

            f.write(10);
            f.flush();
            f.close();
        } catch (IOException var16) {
            var16.printStackTrace();
        }

        file_path = tempFile.getAbsolutePath();
        f = null;

        Process pr;
        try {
            pr = Runtime.getRuntime().exec(this.exe_path + " --file_path " + file_path + " --file_format " + "array");
        } catch (IOException var14) {
            throw new InvalidWaveException("Running exe error", -1);
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = null;
        ArrayList<Double> rawDCDataPoints = new ArrayList();
        ArrayList<Integer> startIndices = new ArrayList();
        ArrayList endIndices = new ArrayList();

        try {
            line = in.readLine();
            if (line.startsWith("Error")) {
                if (line.endsWith("0")) {
                    throw new InvalidWaveException("Reading data error", 0);
                }

                if (line.endsWith("1")) {
                    throw new InvalidWaveException("Wave not long enough", 1);
                }

                if (line.endsWith("2")) {
                    throw new InvalidWaveException("No valid wavelets detected", 2);
                }

                if (line.endsWith("3")) {
                    throw new InvalidWaveException("No valid waveform detected", 3);
                }
            }

            String[] var13;
            int var12 = (var13 = line.split(",")).length;

            String idx;
            int var11;
            for(var11 = 0; var11 < var12; ++var11) {
                idx = var13[var11];
                rawDCDataPoints.add(Double.parseDouble(idx.trim()));
            }

            line = in.readLine();
            var12 = (var13 = line.split(",")).length;

            for(var11 = 0; var11 < var12; ++var11) {
                idx = var13[var11];
                startIndices.add(Integer.parseInt(idx.trim()));
            }

            line = in.readLine();
            var12 = (var13 = line.split(",")).length;

            for(var11 = 0; var11 < var12; ++var11) {
                idx = var13[var11];
                endIndices.add(Integer.parseInt(idx.trim()));
            }

            in.close();
            pr.destroy();
            tempFile.delete();
            return new Wave(rawDCDataPoints, startIndices, endIndices);
        } catch (IOException var15) {
            throw new InvalidWaveException("Running exe error", -1);
        }
    }
}

练习一下:

Java代码:

import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @ClassName: test01
 * @Description: 测试防范
 * @author: 惠涛
 * @date: 2024/4/26 1:35
 */

public class test01 {
    public static void main(String args[]) {
        String exe_path = "H:\\WorkPlace\\JavaPlace\\Hadoop_test\\file\\test.py";
        String file_path = "H:\\WorkPlace\\JavaPlace\\Hadoop_test\\Nfile";
        Process pr = null;
        try {
            pr = Runtime.getRuntime().exec("python " + exe_path + " --file_path " + file_path);
        } catch (IOException e) {
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = null;
        try {
            line = in.readLine();  //读取第一行的数据
            System.out.print(line +"\n");
            line = in.readLine();  //读取第一行的数据
            System.out.print(line+"\n");
        } catch (IOException e) {

        }
    }

}

python代码:test.py

import sys

def hug():
    yu = sys.argv[1:]
    print(yu,end="\n")

    du = "谢谢!"
    les=str.encode(du,"utf-8")
    print(les,end="\n")

hug()

结果:

['--file_path', 'H:\\WorkPlace\\JavaPlace\\Hadoop_test\\Nfile']
b'\xe8\xb0\xa2\xe8\xb0\xa2\xef\xbc\x81'

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值