【问题标题】:Java: Searching For Specific Bytes in a binary file and replacing or omitting said bytesJava:在二进制文件中搜索特定字节并替换或省略所述字节
【发布时间】:2016-03-10 22:02:12
【问题描述】:

我需要在文件中搜索单词“test”。我正在使用的文件是一个文本文件,但我将在二进制文件上使用它。

下面的代码看起来应该对我有用,但它不起作用。我能够在文件中显示“测试”的实例,但我无法让它基本上不在创建的文件中写入“测试”。

有什么帮助吗?

public static void makelabels(){
   File file = new File("test.txt");
   // Check if File Exists.
   if(file.exists()){
     //Do work boy!!!!
   int length = (int) file.length();
   System.out.println("\nFile Length is "+length+" bytes");
   try{
   byte[] bytes = new byte[length];
   int i = 0;
   int count = 0;
   char c;
   FileInputStream input = new FileInputStream(file);
   FileOutputStream output = new FileOutputStream("test2.txt");
   input.read(bytes);
   for(byte b:bytes){
     c = (char) b;
     if(Character.toString(c).equals("t")){
       if(Character.toString((char) bytes[i+1]).equals("e")){
         if(Character.toString((char) bytes[i+2]).equals("s")){
           if(Character.toString((char) bytes[i+3]).equals("t")){
             count++;
             System.out.println("Found TEST " + count +" times");

           }
           else{
             output.write(b);
           }
         }
         else{
           output.write(b);
         }
       }
       else{
         output.write(b);
       }
     }
    else{
      output.write(b);
    }

     i++;
   }
   System.out.println("\n\n");
   System.out.println("Test Results\n\n");
   input.close();
   output.close();

   return;}

   catch(FileNotFoundException ex){
     System.out.println("\nFile Not Found");
   }
   catch(IOException ex){
     System.out.println("\nCan't Read File");
   }
 }
 else{
   System.out.println("\nFile Not Found!");
   return;
 }
 }

感谢你们提供的大量帮助。

我没有遇到数组问题。

这里是测试文件内容。

"this is a test
Please test me"

这是我的结果

"this is a est
Please est me"

该代码对我来说很有意义,并且确实看起来应该可以工作,但我没有任何运气。

【问题讨论】:

  • 您遇到的错误有时比代码墙更重要。
  • 这个代码会得到一个ArrayIndexOutOfBoundsException. Strange,你没有提到。真是莫名其妙。
  • 我已经更新了问题。但是对于 EJP 没有数组问题,先生。无论如何谢谢:)

标签: java binary byte fileinputstream fileoutputstream


【解决方案1】:

我可能会建议以不同的方式处理它,而不是将每个个体转换为一个字节。

byte[] match = "test".getBytes();
for(int i =0; i < 1 + bytes.length - match.length; i++){
  boolean flag = true;
  for(int j= 0; j < match.length; j++){
    if(match[j] != bytes[i+j]){
      flag = false;
      break;
    }
  }
  if(flag){
    count++;
    i+=match.length-1; // don't check these bytes anymore which will also cause them to not be written because it won't do the check below. 
  }else{
     output.write(b);
  }
}

外部循环将遍历可能开始您想要匹配的字符串的每个字节。内部循环将从起点开始并比较以下字节。如果所有字节都匹配,Flag 将保持为真。但是,如果存在差异,则 flag 将设置为 false。因此,如果标志为真,则增加计数。

编辑:如果找到匹配项,上面的代码现在将索引增加您尝试匹配的字符串的长度。它这样做与写入输出相反。据我了解,这就是您要问的问题,但我不确定。如果这不是你想要的,你能再解释一下吗?

【讨论】:

  • 今天晚些时候我会试试这个,让你知道我得到了什么。谢谢楼主:)
  • 我现在就要试试这个。读完这句话,就完全明白了。先生,如果是这样,您会很高兴的。非常感谢。如果有帮助,我会告诉你:0
  • 好吧,攻击方法不同但结果相同:(我怎么能告诉程序甚至不输出字节测试。?我认为这是问题。顺便说一句,非常感谢你比我的代码更简洁。大声笑
  • 再次感谢马特,我发现它确实有效,直到我注意到它没有输出文本文件中的最后几个字母。所以不是“请(空白)我”,而是说“请(空白)”,我确定我做错了什么,谢谢。你帮了大忙
  • 好吧伙计,我只是让 for 循环使用文件的长度,而不是 1 + bytes.length-match.length。不知道为什么要添加它,但我认为我需要的只是文件本身的大小。它有效。
猜你喜欢
  • 2010-12-03
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
相关资源
最近更新 更多