本文实例讲述了Java实现的执行python脚本工具类。分享给大家供大家参考,具体如下:
这里java中执行python脚本工具类,需要使用
java中执行python脚本工具类,学习的时候写着玩:
进口java.io.FileInputStream; 进口java.io.IOException; 进口java.io.InputStream; 进口java.util.ArrayList; 进口并不知道; 进口java.util.Map; 进口org.python.core.PyObject; 进口org.python.util.PythonInterpreter; 最后公共类JythonUtil { 私人JythonUtil () {}/* * *执行某个. py文件 * @param filePath * @throws IOException */公共静态孔隙pythonExecute(字符串filePath)抛出IOException { PythonInterpreter销=new PythonInterpreter (); InputStream是=new FileInputStream (filePath); pin.execfile(是); is.close (); }/* * *获取python程序的变量的值 * @param filePath * @param ponames * @return * @throws IOException */公共静态ListfilePath transP2JData(字符串,字符串…ponames)抛出IOException { PythonInterpreter销=new PythonInterpreter (); InputStream是=new FileInputStream (filePath); pin.execfile(是); is.close (); List ,pos=new ArrayList<的在(); (字符串poname: ponames) { PyObject阿宝=pin.get (poname); pos.add (po); } 返回pos; }/* * *将参数赋给python程序执行 * @param filePath * @param pomaps * @throws IOException */公共静态孔隙transJ2PData (String filePath Map<字符串,Object>pomaps)抛出IOException { PythonInterpreter销=new PythonInterpreter (); InputStream是=new FileInputStream (filePath); (字符串pomapkey: pomaps.keySet ()) { 销。集(pomapkey pomaps.get (pomapkey)); } pin.execfile(是); is.close (); }/* * *将参数赋给python程序执行,并获取python中的变量的值 * @param filePath * @param pomaps * @param ponames * @return * @throws IOException */公共静态List transJ2PData (String filePath Map<字符串,Object>pomaps,字符串…ponames)抛出IOException { PythonInterpreter销=new PythonInterpreter (); InputStream是=new FileInputStream (filePath); (字符串pomapkey: pomaps.keySet ()) { 销。集(pomapkey pomaps.get (pomapkey)); } pin.execfile(是); is.close (); List ,pos=new ArrayList<的在(); (字符串poname: ponames) { PyObject阿宝=pin.get (poname); pos.add (po); } 返回pos; } } >之前 <强>附:强> jython.jar点击此处。
更多的java相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作DOM节点技巧总结》,《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
Java实现的执行python脚本工具类示例【使用jython.jar】