如何在Java中将文件输入分割成多个部分
我需要在下面的文件中分开每个规则。 我如何在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}
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())来提取感兴趣的信息查看并搜索有关正则表达式的基本教程。它们非常强大/有用。感谢您的回复。我已经编辑了我最终需要的案例。您能告诉我如何将每个单独的规则添加到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);
}