java binarywriter,在java中读取C#中的二进制文件

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

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#.

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

java.io.UTFDataFormatException: malformed input around byte 21

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?

解决方案

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.

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值