java - 如何最有效地将二进制文件中的特定字节转换为字符串

标签 java string arrays binary-data

所以我有二进制 FRX 文件,我需要从中提取字符串到 Java 中。
我将其写入我的 Java 程序中,如下所示:

FileInputStream ReadFRX = null ;
FileOutputStream TempCapt = null ;
    try{                
        // refNum is hex number on end of VB form property converted to decimal, ex: $"frmResidency.frx":0134
        int refNum = Integer.parseInt(line.substring(line.length() - 4, line.length()), 16);

        // FRXtemp.txt is created, to temporarily write FRX captions onto to be read from. 
        PrintWriter writer = new PrintWriter("FRXtemp.txt", "UTF-8");
        writer.close();

        //opens corresponding FRX file to read into
        ReadFRX = new FileInputStream("FRXFiles\\"+curFrmName + ".frx");
        //aLittleEndian... must be used to match readInt() little-endianness
        LittleEndianDataInputStream ActReadFRX = new LittleEndianDataInputStream(ReadFRX);
        TempCapt = new FileOutputStream("FRXtemp.txt");

        ActReadFRX.skipBytes(refNum);
        int length = ActReadFRX.readInt();
        int c;

            for (c = 0; c < length; c++) {
                // first read byte and check for EOF
                TempCapt.write(ActReadFRX.read());
            }
        }
//If caption is not read properly (ie. possibly wrong bytes), EOF Exception will occur and designer will break
catch (EOFException e){

    System.out.println("ERROR : FRX Caption property was mishandled");
    break;
}

//Read data from FRXtemp.txt into string
String actCaption = "\"" + new Scanner(new File("FRXtemp.txt")).useDelimiter("\\A").next() + " \" ";

这工作得很好,但是我认为写入临时文件以便我可以读取它是非常没有必要的。

为什么我想不出更有效的方法:
我觉得更实用的方法是使用 Byte[] Array,然后将其转换为字符串,但是我必须只有其中的字节字符串被存储。研究使我相信,RandomAccessFile 是必要的,这样我就可以设置从 ReadInt 开始读取字节的偏移量,但是 RandomAccessFile 假设 big-字节序格式,而我有小字节序格式。我显然可以转换,但是此时我当前的解决方案似乎同样可行。

我的问题是,是否有一种有效的方法来转换与 4 字节整数对应的特定字节部分(来自小端格式的二进制文件) ) 转换为 Java 中的字符串?

我觉得我一定忽略了一些更简单的事情。谢谢:)

最佳答案

您可以通过多种方式执行此操作,但最简单的可能是。

try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) {
    dis.skip(bytesToSkip);
    int length = Integer.reverseBytes(dis.readInt());
    byte[] bytes = new bytes[length];
    dis.readFully(bytes);
    return new String(bytes, "UTF-8");
}

您可能一直在寻找的方法在Integer

/**
 * Returns the value obtained by reversing the order of the bytes in the
 * two's complement representation of the specified {@code int} value.
 *
 * @param i the value whose bytes are to be reversed
 * @return the value obtained by reversing the bytes in the specified
 *     {@code int} value.
 * @since 1.5
 */
public static int reverseBytes(int i) {
    return ((i >>> 24)           ) |
           ((i >>   8) &   0xFF00) |
           ((i <<   8) & 0xFF0000) |
           ((i << 24));
}

关于java - 如何最有效地将二进制文件中的特定字节转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28329098/

相关文章:

java - Astyanax 无法读取某些列族

java - 如何在C、java和C++中编译和运行任意位置的任意文件?

java - 安装Maven 3.3.9,如何设置M2_HOME、MAVEN_HOME和bin文件夹?

java - 修剪字符串末尾的数字

java - 如何提取特定的字符串行

arrays - 傀儡排序是如何工作的?

java - 长时间运行的Web服务架构

swift - 在 Swift 中获取字符串的第 n 个字符

javascript - 需要其他数组中的数组

python - 最好使用元组或 numpy 数组来存储坐标