JAVA - 用标题读取二进制文件(尝试传输c#代码)

时间:2016-10-20 01:53:23

标签: java

我有一些旧的C#代码可以满足我的需要,使用我正在使用的文件类型,但我需要将它转换为Java。我一直在读二进制I / O,但我无法弄清楚如何处理标题,我不明白C#代码足以知道它在做什么

我将非常感谢任何帮助 - 主要是了解C#代码在使用br.readInt32()时的含义以及如何使用Java模拟它(据我所知)以不同的方式读取二进制文件

我不太了解二进制文件(我也不想,这是一个一次性的代码片段),我只想获取数据然后我可以处理我更了解的代码。

感谢

C#片段: [代码]

    public void ConvertEVDtoCSV(string fileName)
    {
        string[] fileArray = File.ReadAllLines(fileName);
        float minX = 0;
        float maxX = 0;

        try
        {
            FileStream fs = new FileStream(fileName, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);

            /*
                16 + n*80*6 = sizeof(header) where n is the 9th nibble of the file (beginning of the 5th byte)
            */

            //Reads "EVIS" 
            br.ReadBytes(4);
            //Reads numDataSets
            int numDataSets = br.ReadInt32();
            //Reads lngNumPlotSurfaces
            int lngNumPlotSurfaces = br.ReadInt32();
            //Reads headerEvisive length
            int headerEvisive = br.ReadInt32();
            //skip all six title and axes text lines.  
            int remainingHeader = (lngNumPlotSurfaces * 6 * 80) + headerEvisive;

            br.ReadBytes(remainingHeader);      //could also use seek(remainingHeader+16), but streams don't support seek?

            long dataSize = numDataSets * (2 + lngNumPlotSurfaces); //meb 6-8-2016: +2 for X and Y
            string[] dataForCSVFile = new string[dataSize];

            for (long cnt = 0; cnt < numDataSets; cnt++)
            {
                for (int j = 0; j < 2 + lngNumPlotSurfaces; j++)    //+2 for X and Y
                    {
                    //don't read past the end of file
                    if (br.BaseStream.Position<br.BaseStream.Length) {
                    //This is where the data needs to be read in and converted from 32-bit single-precision floating point to strings for the csv file
                    float answerLittle = br.ReadSingle();
                    if (j == 0 && answerLittle > maxX)
                        maxX = answerLittle;
                    if (j == 0 && answerLittle < minX)
                        minX = answerLittle;
                    if (j > lngNumPlotSurfaces)
                        dataForCSVFile[cnt * (2 + lngNumPlotSurfaces) + j] = answerLittle.ToString() + "\r\n";
                    else
                        dataForCSVFile[cnt * (2 + lngNumPlotSurfaces) + j] = answerLittle.ToString() + ",";
                    }
                }
            }
            fs.Close();
            textBox_x_max.Text = (maxX).ToString("F2");
            textBox_x_min.Text = (minX).ToString("F2");
            StreamWriter sw = new StreamWriter(tempfile);      

            for (int i = 0; i < dataForCSVFile.Length; i++)
            {
                sw.Write(dataForCSVFile[i]);
            }
            sw.Close(); 
        }
        catch (Exception ex)
        { Console.WriteLine("Error reading data past eof."); }
    }

1 个答案:

答案 0 :(得分:-1)

来自MSDN: https://msdn.microsoft.com/en-us/library/system.io.binaryreader.readint32(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

  

从当前流中读取一个4字节有符号整数,并将流的当前位置提前四个字节。“

https://msdn.microsoft.com/en-us/library/system.io.binaryreader.readsingle(v=vs.110).aspx

  

从当前流中读取的4字节浮点值。

要在Java中读取整数,您还必须注意字节序。 C#BinaryReader默认是小端,而Java是大端。所以当读取整数时,你必须读取4个字节,交换它们的顺序并重新组合它们。