4

嗨团队,我正在尝试在二进制文件中查找字符串“Henry”并将字符串更改为不同的字符串。仅供参考,该文件是对象序列化的输出。原始问题在这里

我是搜索字节的新手,并想象这段代码会搜索我的 byte[] 并交换它。但它并没有接近工作它甚至没有找到匹配项。

{
    byte[] bytesHenry = new String("Henry").getBytes();
    byte[] bytesSwap = new String("Zsswd").getBytes();

    byte[] seekHenry = new byte[bytesHenry.length];

    RandomAccessFile file = new RandomAccessFile(fileString,"rw");

    long filePointer;
    while (seekHenry != null) {
       filePointer = file.getFilePointer();
       file.readFully(seekHenry);
       if (bytesHenry == seekHenry) {
           file.seek(filePointer);
           file.write(bytesSwap);
           break;
       }
     }
}

好的,我看到了bytesHenry==seekHenry问题,将换到Arrays.equals( bytesHenry , seekHenry )

我想我每次读取 5 个字节时都需要移动 -4 个字节的位置。


宾果游戏它现在找到了

    while (seekHenry != null) {
                    filePointer = file.getFilePointer();
                    file.readFully(seekHenry);;
                    if (Arrays.equals(bytesHenry,
                                      seekHenry)) {
                        file.seek(filePointer);
                        file.write(bytesSwap);
                        break;
                    }
                    file.seek(filePointer);
                    file.read();
                }
4

2 回答 2

2

以下可能对您有用,请参阅search(byte[] input, byte[] searchedFor)返回第一个匹配匹配的索引或-1的方法。

public class SearchBuffer {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String charset= "US-ASCII";
        byte[] searchedFor = "ciao".getBytes(charset);
        byte[] input = "aaaciaaaciaojjcia".getBytes(charset);

        int idx = search(input, searchedFor);
        System.out.println("index: "+idx); //should be 8
    }

    public static int search(byte[] input, byte[] searchedFor) {
        //convert byte[] to Byte[]
        Byte[] searchedForB = new Byte[searchedFor.length];
        for(int x = 0; x<searchedFor.length; x++){
            searchedForB[x] = searchedFor[x];
        }

        int idx = -1;

        //search:
        Deque<Byte> q = new ArrayDeque<Byte>(input.length);
        for(int i=0; i<input.length; i++){
            if(q.size() == searchedForB.length){
                //here I can check
                Byte[] cur = q.toArray(new Byte[]{});
                if(Arrays.equals(cur, searchedForB)){
                    //found!
                    idx = i - searchedForB.length;
                    break;
                } else {
                    //not found
                    q.pop();
                    q.addLast(input[i]);
                }
            } else {
                q.addLast(input[i]);
            }
        }

        return idx;
    }
}
于 2014-03-06T21:22:33.490 回答
0

使用 java 在文本文件中查找字符串的最快方法

我在 MIMEParser 中找到的最佳实现:https ://github.com/samskivert/ikvm-openjdk/blob/master/build/linux-amd64/impsrc/com/sun/xml/internal/org/jvnet/mimepull/ MIMEParser.java

/**
  * Finds the boundary in the given buffer using Boyer-Moore algo.
  * Copied from java.util.regex.Pattern.java
  *
  * @param mybuf boundary to be searched in this mybuf
  * @param off start index in mybuf
  * @param len number of bytes in mybuf
  *
  * @return -1 if there is no match or index where the match starts
  */

  private int match(byte[] mybuf, int off, int len) {

还需要:

  private void compileBoundaryPattern();
于 2015-03-01T10:05:02.060 回答