【发布时间】:2026-01-22 14:30:02
【问题描述】:
我是高级编程的新手, 好吧,我知道它非常不专业的问题:-
我喜欢用java解析文件
例如 我的 in.vmf 包含:
vaps graxxx
metagraphic
ccccc cccc
vvvv vvvv
xxx cccc
Begin_object "xxx"
Translate
somethoing
anything
end Begin_object
我想用以下规则解析上面的文件:
- 如果它找到第一行“vaps graxx”,它将继续复制/解析,直到找到 Begin_object。并添加一些行
- 然后搜索其他单词并复制/解析直到某些内容并添加一些行
- 继续进行其他类似搜索等
如果我的代码在这里
BufferedReader FileBuf;
try{
// Open the file and read the file
FileBuf = new BufferedReader(new FileReader("arc.VMF"));
// Create a new file
OutputFile = new PrintWriter(new FileWriter("newarc.VMF"));
// Start reading file Line by Line
while ((LineRead = FileBuf.readLine()) != null) {
// LineStr = LineRead.split("");
if (LineRead.isEmpty()) {
System.out.println("Line is empty");
}
if ((LineRead.startsWith("%"))
&& (LineRead.indexOf("V A P S") > 0)) {
OutputFile.println(LineRead);
}
if (LineRead.indexOf("BEGIN_OBJECT") > 0) {
OutputFile.println(LineRead);
}
.......
.....
....
}
FileBuf.close();
OutputFile.close();
谁能给点建议,会很有帮助的。 谢谢
【问题讨论】:
-
还要看正则表达式。它用于搜索文本中的复杂表达式。 docs.oracle.com/javase/tutorial/essential/regex
标签: java io file-handling