string - java (matcher.group()) - write to file -
i have code finds coordinates between specific words. now, output console works perfect, id output matches found file current code:
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"); } }
output should
world
solid
(3245) (2334) (-234)
.
.
.
.
.
.
(457) (2) (2323)
instead, when output file, first coords appear:
(3245) (2334) (-234)
as written, opening same file through each pass through while loop. each filewriter/bufferedwriter
combo write 1 line of output. none ever closed. when released, it's going guessing game 1 flushed & closed last, overwriting other outputs.
try moving while
loop inside try
, after bufferedwriter
created. , close bf
when done (in finally
block nice).
Comments
Post a Comment