简体   繁体   English

在Java中编辑二进制文件

[英]Editing a binary file in java

I need to do this project where I take a binary file read from it, and then make a new binary file that has adds 00 depending on the offset. 我需要执行此项目,在其中读取一个二进制文件,然后制作一个新的二进制文件,该文件根据偏移量增加了00。 So for example, if the binary number is at offset 02, it will shift that binary number by 2. So that binary number will be at 04. with 00 in between. 因此,例如,如果二进制数位于偏移量02处,它将使该二进制数偏移2。因此,该二进制数将位于04.,中间是00。

I am thinking of using the Random Access File to access it and edit (read only). 我正在考虑使用随机访问文件来访问它并进行编辑(只读)。 But I am lost at by how many bytes do you seek. 但是我迷上了要寻找多少字节。 Also, how do edit and put the modification into a brand new file. 另外,如何进行编辑并将修改内容放入一个全新的文件中。

Kindest Regards 最亲切的问候

这是二进制文件的十六进制格式

在此处输入图片说明

In the image above. 在上图中。 the first byte in blue (0x54) is at 0x40 + 0x0E . 蓝色(0x54)的第一个字节位于0x40 + 0x0E Add the left row value plus the column value. 添加左行值加列值。 So you must read everthing up to but excluding that byte. 因此,您必须读取最多但不包括该字节的所有内容。 So that would be offset 0x40 + 0x0D . 这样就可以偏移0x40 + 0x0D That would be 64 + 13 = 77 . 那将是64 + 13 = 77 Since the offset starts at 0 , read in the first 78 bytes then write out what you want. 由于偏移量从0开始,因此先读取前78个字节,然后写出所需的内容。 Then write out the rest of the file. 然后写出文件的其余部分。 That means all the blue would be written after you special insertion. 这意味着所有蓝色将在您特殊插入后被写入。

This is all presuming that, that is what you want to do. 所有这些都是假定的,那就是您要执行的操作。 But you can't really use seek because that would imply you are modifying the source file which is not wise (since you might make a mistake and need to start over). 但是您不能真正使用seek因为这意味着您修改的是不明智的源文件(因为您可能会犯错并且需要重新开始)。 Also seeking into a file under modification can be very problematic since its size may keep changing. 由于文件的大小可能会不断变化,因此查找正在修改的文件也会非常成问题。 In any event make certain you keep a backup copy of the file. 无论如何,请确保您保留文件的备份副本。

To make further modifications, keep reading and writing as described. 要进行进一步的修改,请保持所述的读写状态。

Here is a brute force example. 这是一个蛮力的例子。 It could be improved with try-with-resources and loops as appropriate. 可以使用try-with-resources和loops进行适当的改进。 Exceptions are presumed to be monitored. 假定将监视异常。 This example simply inserts two triads of integers in specified locations and them prints the new file. 本示例仅在指定位置插入两个三元组整数,它们将打印新文件。

      // create a file to start
      FileOutputStream fo = new FileOutputStream("myfile.txt");
      String alpha = "abcdefghijklmnopqrstuvwxyz";
      fo.write(alpha.getBytes());
      fo.close();

      // create the new output file and open up the old one as an input
      // file
      fo = new FileOutputStream("myfile2.txt");
      FileInputStream fi = new FileInputStream("myfile.txt");
      // insert 123 after k
      byte[] buf = new byte[100];
      int count = fi.read(buf, 0, 11); // read up to and including k
      fo.write(buf, 0, count);
      fo.write("123".getBytes());
      // insert 456 after t
      count = fi.read(buf, 0, 9);
      fo.write(buf, 0, count);
      fo.write("456".getBytes());
      //copy rest of input file to output file
      while ((count = fi.read(buf, 0, 10)) > 0) {
         fo.write(buf, 0, count);
      }
      fo.close();
      fi.close();

      fi = new FileInputStream("myfile2.txt");
      byte[] buffer = fi.readAllBytes();
      for (byte b : buffer) {
         System.out.print((char) b);
      }
      System.out.println();
      fi.close();
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM