但是,我强烈建议您对Java和.NET采取更简单的方法:在Java中,使用 DataInputStream 和 DataOutputStream .无需复杂地使用ByteBuffer等.在.NET中,使用MiscUtil中的EndianBinaryReader,该文件扩展了 BinaryReader (对于 BinaryWriter )或者,考虑只使用文本.I'm into a "compatibility" issue between two versions of the same program, the first one written in Java, the second it's a port in C#.My goal is to write some data to a file (for example, in Java), like a sequence of numbers, then to have the ability to read it in C#. Obviously, the operation should work in the reversed order.For example, I want to write 3 numbers in sequence, represented with the following schema:first number as one 'byte' (4 bit)second number as one 'integer' (32 bit)third number as one 'integer' (32 bit)So, I can put on a new file the following sequence: 2 (as byte), 120 (as int32), 180 (as int32)In Java, the writing procedure is more or less this one:FileOutputStream outputStream;byte[] byteToWrite;// ... initialization....// first byteoutputStream.write(first_byte);// integersbyteToWrite = ByteBuffer.allocate(4).putInt(first_integer).array();outputStream.write(byteToWrite);byteToWrite = ByteBuffer.allocate(4).putInt(second_integer).array();outputStream.write(byteToWrite);outputStream.close();While the reading part it's the following:FileInputStream inputStream;ByteBuffer byteToRead;// ... initialization....// first bytefirst_byte = inputStream.read();// integersbyteToRead = ByteBuffer.allocate(4);inputStream.read(byteToRead.array());first_integer = byteToRead.getInt();byteToRead = ByteBuffer.allocate(4);inputStream.read(byteToRead.array());second_integer = byteToRead.getInt();inputStream.close();C# code is the following. Writing:FileStream fs;byte[] byteToWrite;// ... initialization....// first bytebyteToWrite = new byte[1];byteToWrite[0] = first_byte;fs.Write(byteToWrite, 0, byteToWrite.Length);// integersbyteToWrite = BitConverter.GetBytes(first_integer);fs.Write(byteToWrite, 0, byteToWrite.Length);byteToWrite = BitConverter.GetBytes(second_integer);fs.Write(byteToWrite, 0, byteToWrite.Length);Reading:FileStream fs;byte[] byteToWrite;// ... initialization....// first bytebyte[] firstByteBuff = new byte[1];fs.Read(firstByteBuff, 0, firstByteBuff.Length);first_byte = firstByteBuff[0];// integersbyteToRead = new byte[4 * 2];fs.Read(byteToRead, 0, byteToRead.Length);first_integer = BitConverter.ToInt32(byteToRead, 0);second_integer = BitConverter.ToInt32(byteToRead, 4);Please note that both the procedures works when the same Java/C# version of the program writes and reads the file. The problem is when I try to read a file written by the Java program from the C# version and viceversa. Readed integers are always "strange" numbers (like -1451020...).There's surely a compatibility issue regarding the way Java stores and reads 32bit integer values (always signed, right?), in contrast to C#. How to handle this? 解决方案 It's just an endian-ness issue. You can use my MiscUtil library to read big-endian data from .NET.However, I would strongly advise a simpler approach to both your Java and your .NET:In Java, use DataInputStream and DataOutputStream. There's no need to get complicated with ByteBuffer etc.In .NET, use EndianBinaryReader from MiscUtil, which extends BinaryReader (and likewise EndianBinaryWriter for BinaryWriter)Alternatively, consider just using text instead. 这篇关于如何将数字写入文件并使它们在Java和C#之间可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-18 07:48