在java中读取C#中的二进制文件 [英] reading c# binary files in java

查看:792
本文介绍了在java中读取C#中的二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写1的整数和3串到一个文件中,使用 BinaryWriter.Write() C#.NET有一个程序。

I have a program in C# .net which writes 1 integer and 3 strings to a file, using BinaryWriter.Write().

现在我编程用Java(Android设备,而我在Java中是新),我有机会访问是pviously写入使用C#文件$ P $的数据。

Now I am programming in Java (for Android, and I'm new in Java), and I have to access the data which were previously written to a file using C#.

我试着用 DataInputStream.readInt() DataInputStream.readUTF(),但我不能让正确的结果。我通常会得到一个 UTFDataFormatException

I tried using DataInputStream.readInt() and DataInputStream.readUTF(), but I can't get proper results. I usually get a UTFDataFormatException:

java.io.UTFDataFormatException:约21字节输入格式不正确。

java.io.UTFDataFormatException: malformed input around byte 21

字符串 INT 我得到的是错误的...

or the String and int I get is wrong...

FileInputStream fs = new FileInputStream(strFilePath);
DataInputStream ds = new DataInputStream(fs);
int i;
String str1,str2,str3;
i=ds.readInt();
str1=ds.readUTF();
str2=ds.readUTF();
str3=ds.readUTF();
ds.close();

什么是这样做的正确方法?

What is the proper way of doing this?

推荐答案

顾名思义,的BinaryWriter以二进制格式写入。达网络二进制格式为precise,并作为Java是不.NET语言,它没有读取它的方法。你必须使用一个可互操作的格式。

As its name implies, BinaryWriter writes in binary format. .Net binary format to be precise, and as java is not a .Net language, it has no way of reading it. You have to use an interoperable format.

您可以选择现有的格式,如XML或JSON或任何其他互操作的格式。

You can choose an existing format, like xml or json or any other interop format.

或者你可以创建你自己的,你提供的数据是足够简单,使这种方式(这似乎是这里的情况)。只要写一个字符串到您的文件(使用StreamWriter的举例),只要你知道你的字符串的格式。然后阅读从Java文件作为一个字符串并解析它。

Or you can create your own, providing your data is simple enough to make it this way (it seems to be the case here). Just write a string to your file (using a StreamWriter for instance), provided you know your string's format. Then read your file from java as a string and parse it.

这篇关于在java中读取C#中的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆