如何用Python读取Java中写入的字节文件

huangapple go评论92阅读模式
英文:

How to read a bytes file written in Java with Python

问题

以下是你提供的内容的翻译部分:

在Java(Android)中,我有两个分开的大型short类型和int类型的数组。我想将它们写入文件,然后在Python中读取文件以重新创建数组。问题是,我认为文件在Java中已正确创建和写入,但我无法在Python中正确读取文件。
我在Java中使用以下代码来写入数组。

File mFile = new File(this.getExternalFilesDir(null), "testFile.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(mFile));
objectOutputStream.writeObject(intArray);
objectOutputStream.close();

我还使用以下方法通过循环数组来写入文件(我认为也完全有效)。

File mFile = new File(this.getExternalFilesDir(null), "testFile.txt");
OutputStream outputStream = new FileOutputStream(mFile);
for (int x: intArray) {
    outputStream.write(x);
}
objectOutputStream.close();

我该如何在Python中读取此文件并创建数组。我尝试过参考Python的struct库,但无法使用它。
我尝试了以下代码在Python中读取。

我尝试了一个演示数组
[1000, 1001, 1002, 1003,.....,1025]

with open('../testFile.txt') as mFile:
    for ln in mFile:
        print(list(struct.unpack('I*' (len(ln)//4), ln)))

在print语句处引发错误"error: unpack requires a buffer of 104 bytes"。

英文:

I have two separate large array of type short and int in Java (Android). I want to write them in a file and later read them in Python to recreate the array. The problem is, I think the file is created and written correctly in Java, but I am unable to read the file correctly in Python.
I am using following bits of code in Java to write the array.

File mFile = new File( this.getExternalFilesDir(null),"testFile.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(mFile));
objectOutputStream.writeObject(intArray);
objectOutputStream.close();

I also wrote the file using the following method looping over the array (which I think also works just fine)

File mFile = new File( this.getExternalFilesDir(null),"testFile.txt");
OutputStream outputStream = new FileOutputStream(mFile);
for (int x: intArray) {
    outputStream.write(x);
}
objectOutputStream.close();

How do I read this file and create the array in python. I tried referring the Python struct library but was unable to use it.
I tries the following code to read in Python.
I tried a demo array
[1000, 1001, 1002, 1003,.....,1025]

with open('/../testFile.txt') as mFile:
    for ln in mFile:
        print( list( struct.unpack('I*'(len(ln)//4), ln) ) )

This raise Error "error: unpack requires a buffer of 104 bytes" at the print statement.

答案1

得分: 0

由于Joop Eggen的评论,我修改了Java代码,使用DataOutputStream写入数据,并使用大端格式读取文件(这是我之前没有做到的,Joop Eggen指出了这一点)。以下是Java代码。

short[] mBuffer = {1000, 1001,....,1025};
ByteBuffer byteBuffer = ByteBuffer.allocate(mBuffer.length*2);
ShortBuffer sBuffer = byteBuffer.asShortBuffer();
sBuffer.put(mBuffer);

File mFile = new File(this.getExternalFilesDir(null), "testFile.txt");
OutputStream outputStream = new FileOutputStream(mFile);
outputStream.write(byteBuffer.array());

Python代码用于读取并创建数组。

import struct

with open('../testFile.txt', 'rb') as mFile:
    for ln in mFile:
        # 读取Short数组
        print(list(struct.unpack('>' + 'h'*(len(ln)//2), ln)))
        # 读取Int数组
        print(list(struct.unpack('>' + 'i'*(len(ln)//4), ln)))

# 输出: [1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024]
英文:

Thanks to Joop Eggen's comment I modified the Java code to write data using DataOutputStream and read the file using Big Endian format (something I was not doing and Joop Eggen's pointed out). Here is the Java code.

short[] mBuffer = {1000, 1001,....,1025};
ByteBuffer byteBuffer = ByteBuffer.allocate(mBuffer.length*2);
ShortBuffer sBuffer = byteBuffer.asShortBuffer();
sBuffer.put(mBuffer);

File mFile = new File( this.getExternalFilesDir(null),"testFile.txt");
OutputStream outputStream = new FileOutputStream(mFile);
outputStream.write(byteBuffer.array());

The Python code to read and create the array.

with open('\..\testFile.txt', 'rb') as mFile:
    for ln in mFile:
        # To Read Short Array 
        print( list( struct.unpack('>' + 'h'*(len(ln)//2), ln) ) )
        # To Read Int Array
        print( list( struct.unpack('>' + 'i'*(len(ln)//4), ln) ) )

Output : [1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024]

huangapple
  • 本文由 发表于 2020年9月9日 17:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63808509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定