如何在Java中将文件输入分割成多个部分

如何在Java中将文件输入分割成多个部分,java,delimiter,Java,Delimiter,我需要在下面的文件中分开每个规则。 我如何在Java中做到这一点 这是文件内容 rule apt_regin_2011_32bit_stage1 { meta: copyright = "Kaspersky Lab" description = "Rule to detect Regin 32 bit stage 1 loaders" version = "1.0" last_modified = "2014-11-18" strings: $key1={331015EA261D38A7}

我需要在下面的文件中分开每个规则。 我如何在Java中做到这一点

这是文件内容

rule apt_regin_2011_32bit_stage1 {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin 32 bit stage 1 loaders"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$key1={331015EA261D38A7}
$key2={9145A98BA37617DE}
$key3={EF745F23AA67243D}
$mz="MZ"
condition:
($mz at 0) and any of ($key*) and filesize < 300000
}


rule apt_regin_rc5key {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin RC5 decryption keys"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$key1={73 23 1F 43 93 E1 9F 2F 99 0C 17 81 5C FF B4 01}
$key2={10 19 53 2A 11 ED A3 74 3F C3 72 3F 9D 94 3D 78}
condition:
any of ($key*)
}



rule apt_regin_vfs {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin VFSes"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$a1={00 02 00 08 00 08 03 F6 D7 F3 52}
$a2={00 10 F0 FF F0 FF 11 C7 7F E8 52}
$a3={00 04 00 10 00 10 03 C2 D3 1C 93}
$a4={00 04 00 10 C8 00 04 C8 93 06 D8}
condition:
($a1 at 0) or ($a2 at 0) or ($a3 at 0) or ($a4 at 0)
}


rule apt_regin_dispatcher_disp_dll {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin disp.dll dispatcher"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$mz="MZ"
 $string1="shit"
 $string2="disp.dll"
 $string3="255.255.255.255"
 $string4="StackWalk64"
 $string5="imagehlp.dll"
condition:
($mz at 0) and (all of ($string*))
}
Arraylist[2]

rule apt_regin_vfs {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin VFSes"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$a1={00 02 00 08 00 08 03 F6 D7 F3 52}
$a2={00 10 F0 FF F0 FF 11 C7 7F E8 52}
$a3={00 04 00 10 00 10 03 C2 D3 1C 93}
$a4={00 04 00 10 C8 00 04 C8 93 06 D8}
condition:
($a1 at 0) or ($a2 at 0) or ($a3 at 0) or ($a4 at 0)
}
等等


我怎样才能做到这一点呢?

只是为了记录:如果您的问题仅仅是“分割”输入中的“规则”,那么只需执行以下操作:

List<List<String>> sections = new ArrayList<>();
List<String> currentSection = null;

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  String line;
  while ((line = br.readLine()) != null) {
    if(line.startsWith("rule ")) {
      if (currentSection != null) {
        // we are finished with the previous section!
        sections.add(currentSection);
      }
      currentSection = new ArrayList<>();
      currentSection.add(line);
    } else {
      if(! line.trim().isEmpty()) {
        // any non-empty line goes into the current section
        currentSection.add(line);          
      }
    }
 }
} // end of try/while ... I am too lazy to count my braces ;-)
if (currentSelection != null) {
  // make sure to add the final section, too!
  sections.add(currentSelection); 
}
List sections=new ArrayList();
List currentSection=null;
try(BufferedReader br=new BufferedReader(new FileReader(file))){
弦线;
而((line=br.readLine())!=null){
if(第行开始使用(“规则”)){
如果(currentSection!=null){
//我们完成了上一节!
节。添加(当前节);
}
currentSection=新的ArrayList();
currentSection.add(行);
}否则{
如果(!line.trim().isEmpty()){
//任何非空行都会进入当前节
currentSection.add(行);
}
}
}
}//尝试结束/while。。。我懒得数牙套;-)
如果(currentSelection!=null){
//确保添加最后一部分!
节。添加(当前选择);
}
但是:你对你的真实需求不是很精确。我很确定你真正的问题不是“分割”那个输入文件

最有可能的是,您的实际任务是读取该文件,对于该文件中的每个部分,您需要获取其部分/全部内容以进行进一步处理

换句话说:您实际上是在问“我如何解析/处理”这个输入。我们无法回答这个问题;因为你没有告诉我们你想用这些数据做什么

本质上,这是您的选择空间:

  • 若真的有这样一个固定的布局,那个么“解析”可以归结为理解“首先是规则,然后是元,看起来像……”。意思是:您可以将数据结构“硬编码”到代码中。示例:您完全“知道”第三行包含
    copyright=“some value”
    。然后开始使用正则表达式(或简单的字符串方法,如indexOf()、substring())来提取感兴趣的信息
  • 如果文件格式实际上是某种“标准”(如XMl、JSON、YAML等),那么您可以选择一些第三方库来解析这些文件。以你为例。。。我不能说;这绝对不是我熟悉的格式
  • 最糟糕的情况是,您需要编写自己的解析器。编写解析器是一个复杂但“经过充分研究”的主题,参见示例
    查看并搜索有关正则表达式的基本教程。它们非常强大/有用。感谢您的回复。我已经编辑了我最终需要的案例。您能告诉我如何将每个单独的规则添加到arraylist中吗?请查看我的更新答案。我输入了一些代码来给你一些方法。请注意:此代码未编译/测试;不要只是盲目地复制/粘贴它。逐行阅读,直到你明白它应该做什么;然后相应地修改您自己的代码!好极了你的java真的很好。投票支持你的解决方案
    rule apt_regin_vfs {
    meta:
    copyright = "Kaspersky Lab"
     description = "Rule to detect Regin VFSes"
     version = "1.0"
     last_modified = "2014-11-18"
    strings:
    $a1={00 02 00 08 00 08 03 F6 D7 F3 52}
    $a2={00 10 F0 FF F0 FF 11 C7 7F E8 52}
    $a3={00 04 00 10 00 10 03 C2 D3 1C 93}
    $a4={00 04 00 10 C8 00 04 C8 93 06 D8}
    condition:
    ($a1 at 0) or ($a2 at 0) or ($a3 at 0) or ($a4 at 0)
    }
    
    List<List<String>> sections = new ArrayList<>();
    List<String> currentSection = null;
    
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
      String line;
      while ((line = br.readLine()) != null) {
        if(line.startsWith("rule ")) {
          if (currentSection != null) {
            // we are finished with the previous section!
            sections.add(currentSection);
          }
          currentSection = new ArrayList<>();
          currentSection.add(line);
        } else {
          if(! line.trim().isEmpty()) {
            // any non-empty line goes into the current section
            currentSection.add(line);          
          }
        }
     }
    } // end of try/while ... I am too lazy to count my braces ;-)
    if (currentSelection != null) {
      // make sure to add the final section, too!
      sections.add(currentSelection); 
    }