1 回答
TA贡献1779条经验 获得超6个赞
你的描述不清楚,有些要求看起来很奇怪(你为什么要创建像one.txt, 而不是1.txt)这样的文件?但这只是一个小小的手指练习。也许这会有所帮助?
// Iterator<String> fileNames = Arrays.asList("one.txt", "two.txt", "three.txt", "four.txt", "five.txt", "six.txt", "seven.txt").iterator();
int i = 1;
Pattern start = Pattern.compile("\\(AP9000\\)");
Pattern end = Pattern.compile("\\(NNNN\\)");
boolean reading = false;
FileOutputStream fos = null;
Iterator<String> lines = Files.lines(Paths.get("c:/d/test.txt")).iterator();
while (lines.hasNext() /* && fileNames.hasNext() */) {
String line = lines.next();
Matcher startMatcher = start.matcher(line);
if (startMatcher.find()) {
reading = true;
fos = new FileOutputStream(String.valueOf(i++) + ".txt"/*fileNames.next()*/);
}
if (reading) {
fos.write(line.getBytes(StandardCharsets.UTF_8));
fos.write('\n');
}
Matcher endMatcher = end.matcher(line);
if (endMatcher.find()) {
if (fos != null) {
fos.close();
}
reading = false;
}
}
if (fos != null) {
fos.close();
}
添加回答
举报