Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:在二进制文件中搜索特定字节,并替换或省略所述字节_Java_Binary_Byte_Fileinputstream_Fileoutputstream - Fatal编程技术网

Java:在二进制文件中搜索特定字节,并替换或省略所述字节

Java:在二进制文件中搜索特定字节,并替换或省略所述字节,java,binary,byte,fileinputstream,fileoutputstream,Java,Binary,Byte,Fileinputstream,Fileoutputstream,我需要在文件中搜索单词“test”。我正在使用的文件是一个文本文件,但我将在二进制文件上使用它 下面的代码看起来应该对我有用,但它不起作用。我能够在文件中显示“test”的实例,但我不能让它基本上不在创建的文件中写入“test” 需要帮忙吗 public static void makelabels(){ File file = new File("test.txt"); // Check if File Exists. if(file.exists()){ //Do

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

下面的代码看起来应该对我有用,但它不起作用。我能够在文件中显示“test”的实例,但我不能让它基本上不在创建的文件中写入“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"

这段代码对我来说很有意义,看起来确实应该可以工作,但我运气不好。

我可能建议用不同的方法来处理它,而不是将每个单独的代码转换为一个字节

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);
  }
}
byte[]match=“test”.getBytes();
对于(int i=0;i<1+bytes.length-match.length;i++){
布尔标志=真;
对于(int j=0;j
外部循环将遍历可能以您想要匹配的字符串开头的每个字节。内部循环将从起始点开始,并比较以下字节。如果所有字节都匹配,则标志将保持为true。但是,如果存在差异,则标志将设置为false。因此,如果标志为true,则增加计数


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

您遇到的错误有时比一堵代码墙更重要。此代码将获得一个
ArrayIndexOutOfBoundsException。
奇怪的是,您没有提到这一点。真是莫名其妙。我已经更新了问题。但是对EJP来说没有阵列问题,先生。无论如何谢谢:)今天晚些时候我会试试这个,让你知道我得到了什么。谢谢你,先生:)我现在就要试试这个。读了这篇文章后,它就有了完美的意义。如果是这样的话,你会让我高兴的,先生。非常感谢。如果有帮助,我会告诉您:0好吧,不同的攻击方法,但结果相同:(我怎么能告诉程序甚至不要输出字节t?我想这就是问题所在。顺便说一句,非常感谢你提供了比我更干净的代码。再次感谢你,马特,我发现它一直有效,直到我注意到它没有输出文本文件中的最后两个字母。因此,它不是说“请(空白)我”,而是说“请”(blank)“我肯定我做错了什么,谢谢你。你帮了我大忙OK,我只是让for循环使用文件的长度,而不是1+bytes.length-match.length。我不确定为什么要添加它,但我想我需要的只是文件本身的大小。它可以工作。