寻找另一个文件中的偏移量和文件结束位置

huangapple go评论69阅读模式
英文:

Find offset and eof file into another file

问题

好的,以下是翻译好的内容:

byte[] header = new byte[] {(byte) 0x89,
                            (byte) 0x50,
                            (byte) 0x4E,
                            (byte) 0x47,
                            (byte) 0x0D,
                            (byte) 0x0A,
                            (byte) 0x1A};

byte[] endBytes = new byte[] {(byte) 0x49,
                              (byte) 0x45,
                              (byte) 0x4E,
                              (byte) 0x44,
                              (byte) 0xAE,
                              (byte) 0x42,
                              (byte) 0x60,
                              (byte) 0x82};

RandomAccessFile file = new RandomAccessFile("MY FILE", "r");

从此处开始,我不知道如何继续。

如何获取位于头部和endBytes之间的字节字符串?

英文:

Good Morning. There is a file, which contains several images inside it. How do I get the byte chain between two points I have. For example:

Bytes of my file

89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 
44 52 00 00 00 40 00 00 00 40 08 02 00 00 
00 25 0B E6 89 00 00 00 09 70 48 59 73 00 
00 0E C4 00 00 0E 49 45 4E 44 AE 42 60 82

Code Java

byte[] header = new byte[] {(byte) 0x89,
                            (byte) 0x50,
                            (byte) 0x4E,
                            (byte) 0x47,
                            (byte) 0x0D,
                            (byte) 0x0A,
                            (byte) 0x1A};

byte[] endBytes = new byte[] {(byte) 0x49,
                              (byte) 0x45,
                              (byte) 0x4E,
                              (byte) 0x44,
                              (byte) 0xAE,
                              (byte) 0x42,
                              (byte) 0x60,
                              (byte) 0x82};

RandomAccessFile file = new RandomAccessFile("MY FILE", "r");

From this point forward I don't know how to proceed.

How do I get the byte string contained between the header and the endBytes ?

答案1

得分: 1

自Java 7以来,有一个名为java.nio.file.Files的类,其中有一个[静态]方法叫做readAllBytes()。该方法有一个参数,即要读取的文件的路径,并返回一个包含整个文件内容的byte[]数组。因此不需要使用RandomAccessFile

您想要获得文件中除了前7个字节和最后8个字节之外的所有字节。以下代码进行了演示。

/*
 * import java.nio.file.Files;
 * import java.nio.file.Paths;
 */
try {
    byte[] readBytes = Files.readAllBytes(Paths.get("datafile.dat"));
    byte[] requiredBytes = new byte[readBytes.length - 7 - 8];
    System.arraycopy(readBytes, 7, requiredBytes, 0, requiredBytes.length);
}
catch (IOException xIo) {
    xIo.printStackTrace();
}
英文:

Since Java 7 there is class java.nio.file.Files which has [static] method readAllBytes(). That method has a single parameter which is the path to the file to read and returns a byte[] array that contains the entire file contents. So no need for RandomAccessFile.

You want all the bytes in the file apart from the first seven and the last eight. The following code demonstrates.

/*
 * import java.nio.file.Files;
 * import java.nio.file.Paths;
 */
try {
    byte[] readBytes = Files.readAllBytes(Paths.get("datafile.dat"));
    byte[] requiredBytes = new byte[readBytes.length - 7 - 8];
    System.arraycopy(readBytes, 7, requiredBytes, 0, requiredBytes.length);
}
catch (IOException xIo) {
    xIo.printStackTrace();
}

huangapple
  • 本文由 发表于 2020年10月26日 23:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64540590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定