我想使用 Java Scanner 对象将文件行“块”读入对象。分隔符需要包含在令牌中。我尝试使用正则表达式lookbehind,但分隔符是可变长度的。有没有人有获得分隔符的建议或替代方法?
我目前拥有的 Java 代码(错误地删除了“处理值”行):
ArrayList<ProcessedData> stack = new ArrayList<ProcessedData>();
Scanner scanner = new Scanner(new File("/home/user/data.txt"));
Pattern pattern = Pattern.compile("Processed Value.+?\\n+", Pattern.UNIX_LINES);
scanner.useDelimiter(pattern);
while(scanner.hasNext()) {
stack.add(new ProcessedData(scanner.next()));
}
示例文件:
Id: 1
Raw Value: 1234
Processed Value{423}: A3s2344
Id: 36
Raw Value: 389001
Processed Value{2}: "Access Success"
Id: 28934
Raw Value: 2402
Processed Value: 1345.2 seconds