我有一个文本文件转储,需要将其转换为分隔文件。该文件包含一系列格式如下的“记录”(因为没有更好的词):
User: abc123
Date: 7/3/12
Subject: the foo is bar
Project: 123456
Problem: foo bar in multiple lines of text
Resolution: foo un-barred in multiple lines of text
User: abc123
Date: 7/3/12
Subject: the foo is bar
Project: 234567
Problem: foo bar in multiple lines of text
which may include <newline> and
extend to multiple lines of text
Resolution: foo un-barred in multiple lines of text
...
现在,使用 Java,我使用 StringBuffer 逐行读取此文件,根据一系列if(inputLine.toLowerCase().startsWith("user:"))
逻辑将行解析为单个字段,以将最终分隔行输出到文本文件。
但是,字段Problem
和Resolution
是自由格式的,并且可能是多行的。我正在尝试做一些会创建两个字符串的事情:附加所有行之后Problem:
并结束于,Resolution:
并附加所有行之后开始Resolution:
和结束于Form:
。
我已经查看了这个链接和这个链接,这表明这StringBuilder
可能是一种合适的方式来做到这一点......但是,我不太确定如何构建逻辑。
编辑: 因为我正在逐行阅读,所以我很难理解如何编码
<pseudocode>
If the line starts with "Problem" extract the charactes after "Problem" else
if the PRIOR line starts with "problem" and the current line doesnt start with "resolution" then append characters in line to prior line
etc.
</pseudocode>
但是,如果有第三行“问题......?我只是无法想象如何让它工作。
有什么想法或替代方法可以实现我想要的结果?