gpt4 book ai didi

java - 如何在Java中unpickle文件?

转载 作者:行者123 更新时间:2023-12-01 10:21:06 26 4
gpt4 key购买 nike

我有一个由 Python 进程创建的“pickle”格式数据文件(实际上是 MCDungeon 缓存文件)。我想从 Java 程序中读取此内容。

为此,我使用了以下代码:

    public HashMap<String, Object> getDataFileStream(String filename) {
HashMap<String, Object> data = new HashMap<String, Object>();
File f = new File(filename);
InputStream fs = null;
try {
fs = new FileInputStream(f);
} catch (FileNotFoundException e) {
Log.warning("Pickle file '"+filename+"' not found!");
return null;
}

PyFile picklefile = new PyFile(fs);
PyDictionary phash = null;
try {
phash = (PyDictionary) cPickle.load(picklefile);
} catch ( PyException e3 ) {
log.severe("Cannot unpickle! (Python error)");
e3.printStackTrace(); // throws a null pointer exception
return null;
} catch ( Exception e ) {
log.severe("Cannot unpickle! Err: " + e.getClass().getName());
e.printStackTrace();
return null;
}
ConcurrentMap<PyObject, PyObject> aMap = phash.getMap();
for (Map.Entry<PyObject, PyObject> entry : aMap.entrySet()) {
String keyval = entry.getKey().toString();
PyObject tileentity = (PyList) entry.getValue();
try {
data.put(keyval, pythonToJava(tileentity));
} catch (Exception e) {
data.put(keyval, tileentity);
}
}
return data;
}

我已经包含了 JYthon 库,以及 pythonToJava 函数(其他地方)。

我传递的文件绝对是一个有效的 Picklefile,因为 Python 进程可以成功读取它。

但是,当运行这个函数时,我在cPickle.load中抛出了一个PyException。函数,并在给定 NullPointer 异常的情况下调用 printStackTrace(第 71 行对应于上面的 e3.printStackTrace() 行)

[12:42:18 ERROR]: [DynmapMCDungeon] Cannot unpickle! (Python error)
java.lang.NullPointerException
at org.steveshipway.dynmap.PickleLoader.getDataFileStream(PickleLoader.j
ava:71) ~[?:?]
at org.steveshipway.dynmap.Dungeon.getDungeons(Dungeon.java:28) ~[?:?]
at org.steveshipway.dynmap.DynmapMCDungeon.activate(DynmapMCDungeon.java
:179) ~[?:?]

当我手动加载 Pickle 数据并传递给函数时,我在 cPickle.load 函数中收到 NullPointerException 错误:

[13:56:57 INFO]: [DynmapMCDungeon] Reading in MCDungeon pickle...
[13:56:57 ERROR]: [DynmapMCDungeon] Cannot unpickle the MCDungeon cache! Err: java.lang.NullPointerException
[13:56:57 WARN]: java.lang.NullPointerException
[13:56:57 WARN]: at java.util.Objects.requireNonNull(Unknown Source)
[13:56:57 WARN]: at java.util.Arrays$ArrayList.<init>(Unknown Source)
[13:56:57 WARN]: at java.util.Arrays.asList(Unknown Source)
[13:56:57 WARN]: at org.python.core.PyList.<init>(PyList.java:52)
[13:56:57 WARN]: at org.python.core.PyList.<init>(PyList.java:64)
[13:56:57 WARN]: at org.python.modules.cPickle$Unpickler.load_empty_list(cPickle.java:1909)
[13:56:57 WARN]: at org.python.modules.cPickle$Unpickler.load(cPickle.java:1620)
[13:56:57 WARN]: at org.python.modules.cPickle.load(cPickle.java:636)
[13:56:57 WARN]: at org.steveshipway.dynmap.PickleLoader.getDataFileStream(PickleLoader.java:64)

我的问题是:

  1. 为什么当我尝试打印堆栈跟踪时会收到错误?

  2. 加载 picklefile 时我做错了什么?有没有更好的方法来实现这一目标?

提前感谢任何指针(最好不是空指针,我已经有足够的指针了!)

最佳答案

为了 unpickle .pkl 文件,我使用了 Jython。 Jython 在 Java 中提供了 python2 支持,pickle 文件应使用协议(protocol)代码 0,1 或 2 进行序列化。

{
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("Your Python code here");

PyObject getAtrsFunc = interpreter.get("your_python_function_name");
PyObject funcRes = getImageAtrsFunc.__call__(new PyString("Example_input_String_to_py_function"), new PyInteger(javaIntegerValue));
//Lets say function returns integer array as an output
//To convert output to java usable object
int [] resAtrs = (int []) funcRes.__tojava__(int [].class);
}

在maven项目中使用jython添加

<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>

到你的pom文件

关于java - 如何在Java中unpickle文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35616060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com