zoukankan      html  css  js  c++  java
  • java调用python脚本 并传参

    java:

    package com.mybatis.plus.utils;
    
    import cn.hutool.core.lang.Console;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class test1 {
        public static void main(String[] args) {
    
            String[] arguments = new String[] {"python", "D://workPlace/git-project/py-test/MyTest2.py","9895656"};
            try {
                Process process = Runtime.getRuntime().exec(arguments);
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),
                        "GBK"));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                //java代码中的process.waitFor()返回值为0表示我们调用python脚本成功,
                //返回值为1表示调用python脚本失败,这和我们通常意义上见到的0与1定义正好相反
                int re = process.waitFor();
                System.out.println(re);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    python:

    from enum import Enum
    import sys
    
    class Solution:
        def reverse(self, x: int) -> int:
            if x==0:
                return 0
            strList = []
            isPositive = True
            if x < 0:
                isPositive = False
            value = abs(x)
            temp = value
            while (temp/10.0>0.09):
                i = temp%10
                strList.append(str(i))
                temp = temp//10
            join = "".join(strList)
            result = int(join)
            if result>2147483647:
                result=0
            if isPositive == False:
                result = -result
            return result
    
    if __name__ == "__main__":
        so = Solution()
        print("输入参数:"+sys.argv[1])
        print(so.reverse(int(sys.argv[1])))

    执行java结果:

  • 相关阅读:
    需求分析作业01
    2016年秋季个人阅读计划
    个人总结
    梦断代码读后感03
    进度条16
    我的第一个小系统总结
    进度条15
    eclipse连接SqlServer2008(被它搞得惨兮兮)
    梦断代码读后感02
    Android ViewPager刷新解析
  • 原文地址:https://www.cnblogs.com/guanxiaohe/p/14172018.html
Copyright © 2011-2022 走看看