1. Import the Jython.jar package in the Java project of Eclipse or MyEclipse. http://down.51cto.com/data/2094597
2. Write a tool class that reads Python.
import org.python.util.pythoninterpreter;import java.io.fileinputstream;import Java.io.ioexception;import java.io.inputstream;import java.util.arraylist;import java.util.list ; import java.util.map;import org.python.core.pyobject;public final class jythonutil { private jythonutil () {} /** * execute a. py file * @param filePath * @throws IOException */ public static void pythonexecute (String FilePath) throws IOException{ PythonInterpreter Pin = new pythoninterpreter (); inputstream Is = new fileinputstream (filEpath); pin.execfile (IS); is.close (); } /** * get variable values for Python programs * @param filepath * @param ponames * @return * @throws IOException */ public static list<pyobject> transp2jdata (String filepath, string ... Ponames) throws IOException{ PythonInterpreter Pin = new pythoninterpreter (); inputstream Is = new fileinputstream (FilePath); pin.execfile (IS); is.close (); list<pyobject> pos = new arraylist (); for (String poname : ponames) { pyobject po = pin.get (Poname); Pos.add (PO); } return pos; } /** * assigning parameters to Python program execution * @param filePath * @param pomaps * @throws IOException */ public static void transj2pdata (String filepath, map&Lt String, object> pomaps) throws IOException { pythoninterpreter pin = new pythoninterpreter (); inputstream is = new fileinputstream (FilePath); for (String pomapkey : pomaps.keyset ()) { pin.set (Pomapkey, pomaps.get (pomapkey)); } pin.execfile (IS); is.close (); } /** * assigns the parameter to the Python program execution and gets the value of the variable in Python * @param filePath * @param pomaps * @param ponames * @return * @ Throws ioexception */ public static list <pyobject> transj2pdata (String filepath, map<string, object> pomaps, string...ponames) throws IOException { Pythoninterpreter pin = new pythoninterpreter (); inputstream is = new fileinputstream (FilePath); for (String pomapkey : pomaps.keyset ()) { pin.set (Pomapkey, pomaps.get (pomapkey)); } pin.execfile (IS); &nBsp;is.close (); list<pyobject> pos = new arraylist (); for (string poname : Ponames) { pyobject po = pin.get (Poname); pos.add (PO); } return pos; } }
3. Test whether the Python script calling *.py can be used
Import Java.io.ioexception;public class Jythonutiltest {/** * @param args */public static void main (string[] args) {String Path = "e:\\pythonscript\\hello.py"; try {//) executes *.py script through the Pythonexecute method of the tool class, passing in the file path Jythonutil.pythonexecute (path); SYSTEM.OUT.PRINTLN ("Successful Execution");} catch (IOException e) {e.printstacktrace ();}}}
4. Execution Result:
1. The contents of Hello.pyweb file are as follows:
Print ("Hello");
2. Execution Result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/46/wKioL1X5JKzDn8H1AACZcx10m_8352.jpg "title=" Qq20150916160351.png "alt=" Wkiol1x5jkzdn8h1aaczcx10m_8352.jpg "/>
This article is from the "Listen to the Rain Hope Eternal" blog, please be sure to keep this source http://yongguang.blog.51cto.com/9153118/1695375
Java read-write Python script