如何最有效地将特定字节从二进制文件转换为字符串
How to convert specific bytes from binary file into string most efficiently
所以我有二进制 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
假定大端格式,而我有小端格式。我显然可以转换,但那时我当前的解决方案似乎同样可行。
我的问题是,是否有一种有效的方法来转换对应于 4 字节整数的特定字节部分(来自小端格式的二进制文件)转换成 Java 中的字符串?
我觉得好像我必须忽略一些更简单的事情。谢谢:)
也许是这样的?
long length = 0xff && mybytes[0]; length<<8;
length |= 0xff && mybytes[1]; length<<8;
length |= 0xff && mybytes[2]; length<<8;
length |= 0xff && mybytes[3]; length<<8;
您可以通过多种方式执行此操作,但最简单的可能是。
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));
}
您可以使用您拥有的 inputStream 作为源,并在根据需要创建字符串时使用 ByteBuffer 更正字节序。这将是最有效的方法。
所以我有二进制 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
假定大端格式,而我有小端格式。我显然可以转换,但那时我当前的解决方案似乎同样可行。
我的问题是,是否有一种有效的方法来转换对应于 4 字节整数的特定字节部分(来自小端格式的二进制文件)转换成 Java 中的字符串?
我觉得好像我必须忽略一些更简单的事情。谢谢:)
也许是这样的?
long length = 0xff && mybytes[0]; length<<8;
length |= 0xff && mybytes[1]; length<<8;
length |= 0xff && mybytes[2]; length<<8;
length |= 0xff && mybytes[3]; length<<8;
您可以通过多种方式执行此操作,但最简单的可能是。
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));
}
您可以使用您拥有的 inputStream 作为源,并在根据需要创建字符串时使用 ByteBuffer 更正字节序。这将是最有效的方法。