我有代码可以找到特定单词之间的坐标。现在,输出到控制台完美无缺,但我喜欢将找到的匹配输出到文件我当前的代码:
public class Filetostring {
public static void main(String[] args) throws FileNotFoundException, IOException {
String s = new Scanner(new File("input.txt")).useDelimiter("\\Z").next();
//System.out.println(content);
Pattern patt;
patt = Pattern.compile("\\bworld\\b|\\bsolid\\b|.(-?\\d+\\s-?\\d+\\s-?\\d+\\)\\s\\ (-?\\d+\\s-?\\d+\\s-?\\d+\\)\\s\\(-?\\d+\\s-?\\d+\\s-?\\d+).");
Matcher matcher = patt.matcher(s);
while(matcher.find())
//System.out.println(matcher.group());
try (FileWriter file2 = new FileWriter("output.txt");
BufferedWriter bf = new BufferedWriter(file2)) {
bf.write(matcher.group());
}
System.out.println("Done");
}
}
输出应该是
世界
坚硬的
(3245) (2334) (-234)
.
.
.
.
.
.
(457) (2) (2323)
相反,当我输出到文件时,只出现第一个坐标:
(3245) (2334) (-234)