在java中读取c#二进制文件

在java中读取c#二进制文件,c#,java,binary,C#,Java,Binary,我在C#.net中有一个程序,它使用BinaryWriter.Write()将1个整数和3个字符串写入一个文件 现在我正在用Java编程(对于Android,我是Java新手),我必须访问以前使用C#写入文件的数据 我尝试使用了DataInputStream.readInt()和DataInputStream.readUTF(),但没有得到正确的结果。我通常会得到一个UTFDataFormatException: java.io.UTFDataFormatException:字节21周围的输入格

我在C#.net中有一个程序,它使用
BinaryWriter.Write()
将1个整数和3个字符串写入一个文件

现在我正在用Java编程(对于Android,我是Java新手),我必须访问以前使用C#写入文件的数据

我尝试使用了
DataInputStream.readInt()
DataInputStream.readUTF()
,但没有得到正确的结果。我通常会得到一个
UTFDataFormatException

java.io.UTFDataFormatException:字节21周围的输入格式错误

或者我得到的
字符串
int
是错误的

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();

这样做的正确方式是什么

顾名思义,BinaryWriter是以二进制格式编写的。Net二进制格式,而且由于java不是.Net语言,所以它无法读取它。您必须使用可互操作的格式

您可以选择现有格式,如xml或json或任何其他互操作格式


或者,您可以创建自己的,前提是您的数据足够简单,可以这样做(这里似乎就是这样)。只要在文件中写入一个字符串(例如使用StreamWriter),前提是您知道字符串的格式。然后以字符串形式从java读取文件并对其进行解析。

在这个问题中,BinaryWriter使用的格式有一个很好的解释,应该可以使用ByteArrayInputStream读取数据并编写一个简单的转换器。

我写了一个关于如何用java读取.net的BinaryWriter格式的快速示例

摘自链接:

   /**
 * Get string from binary stream. >So, if len < 0x7F, it is encoded on one
 * byte as b0 = len >if len < 0x3FFF, is is encoded on 2 bytes as b0 = (len
 * & 0x7F) | 0x80, b1 = len >> 7 >if len < 0x 1FFFFF, it is encoded on 3
 * bytes as b0 = (len & 0x7F) | 0x80, b1 = ((len >> 7) & 0x7F) | 0x80, b2 =
 * len >> 14 etc.
 *
 * @param is
 * @return
 * @throws IOException
 */
public static String getString(final InputStream is) throws IOException {
    int val = getStringLength(is);

    byte[] buffer = new byte[val];
    if (is.read(buffer) < 0) {
        throw new IOException("EOF");
    }
    return new String(buffer);
}

/**
 * Binary files are encoded with a variable length prefix that tells you
 * the size of the string. The prefix is encoded in a 7bit format where the
 * 8th bit tells you if you should continue. If the 8th bit is set it means
 * you need to read the next byte.
 * @param bytes
 * @return
 */
public static int getStringLength(final InputStream is) throws IOException {
    int count = 0;
    int shift = 0;
    boolean more = true;
    while (more) {
        byte b = (byte) is.read();
        count |= (b & 0x7F) << shift;
        shift += 7;
        if((b & 0x80) == 0) {
            more = false;
        }
    }
    return count;
}
/**
*从二进制流获取字符串。>因此,如果len<0x7F,则它被编码在1上
*如果len<0x3FFF,则字节b0=len>在2个字节上编码为b0=(len
*&0x7F)| 0x80,b1=len>>7>如果len<0x 1ffff,则在3上对其进行编码
*字节为b0=(len&0x7F)| 0x80,b1=((len>>7)&0x7F)| 0x80,b2=
*len>>14等。
*
*@param是
*@返回
*@抛出异常
*/
公共静态字符串getString(最终输入流为)引发IOException{
int val=getStringLength(is);
字节[]缓冲区=新字节[val];
如果(正在读取(缓冲区)<0){
抛出新IOException(“EOF”);
}
返回新字符串(缓冲区);
}
/**
*二进制文件使用可变长度前缀进行编码,该前缀告诉您
*字符串的大小。前缀以7比特格式编码,其中
*第8位告诉您是否应该继续。如果设置了第8位,则表示
*您需要读取下一个字节。
*@param字节
*@返回
*/
公共静态int getStringLength(最终输入流为)引发IOException{
整数计数=0;
int-shift=0;
布尔更多=真;
而(更多){
字节b=(字节)是.read();

count |=(b&0x7F)你用什么编码编写文件?你必须使用相同的编码来读取它们。如果你打算跨平台读取数据,你应该使用一种可互操作的格式。你想在你的答案中添加一个摘录吗?因此,如果链接消失,你在这里的答案仍然有用?好主意,我将它添加到了我的原始帖子中。