file io - Java - Using multiple PrintWriter but saves only last println -
i'm designing program split data stored in text file 2 separate files based on label of data.
here small version of data.
0,1,2,normal. 5,5,5,strange. 2,1,3,normal.
i use class store each line sample. class parses line store last value label. encapsulated each line object, because intend add features later.
here code sample
class
import java.util.scanner; public class sample { string[]str_vals = new string[3]; string label; sample(scanner line) { (int i=0; i<3; i++) { str_vals[i] = line.next(); } label = line.next(); } string getvalsforcsv() { stringbuilder retval = new stringbuilder(); (int i=0; i<3; i++) { retval.append(str_vals[i]).append(","); } retval.append(label).append("."); return retval+""; } string getlabel() { return label; } }
below code in question. separator
class.
import java.io.*; import java.util.scanner; public class separator { public static final string datafile = "src/etc/test.txt"; public static void main(string[] args) throws filenotfoundexception { rundata(); } public static void rundata() throws filenotfoundexception { try (scanner in = new scanner(new file(datafile))) { // kddcup file uses '.\n' @ end of each line // setting delimiter consume period in.usedelimiter("[.]\r\n|[.]\n|\n"); sample curr; while(in.hasnext()) { // line hold fields single sample scanner line = new scanner(in.next()); line.usedelimiter(", *"); curr = new sample(line); try ( printwriter positive = new printwriter(new file(datafile+"-pos")); printwriter negative = new printwriter(new file(datafile+"-neg")); ) { if (curr.getlabel().equals("normal")) { positive.println("good"); } else { negative.println("bad"); } } } } } }
this issue experiencing code saves last sample
seen respective file. above data test.txt-neg
empty , test.txt-pos
have single line good
; not have 2 good
's expected.
if modify test.txt
data include first 2 lines, files states reversed (i.e. test.txt-neg
has bad
, test.txt-pos
empty). please explain me going on, , how fix error?
because error pointed out in comment. wanted give credit kevino , elliott frisch solution.
as mentioned, i'm creating new printwriter
each time , creating printwriter
in it's default mode of overwriting file. result saves both files based on single sample.
to correct error, have pulled out instantiations of printwriter
in try-with-resource block of scanner
object
import java.io.*; import java.util.scanner; public class separator { public static final string datafile = "src/etc/test.txt"; public static void main(string[] args) throws filenotfoundexception { rundata(); } public static void rundata() throws filenotfoundexception { try ( scanner in = new scanner(new file(datafile)); printwriter positive = new printwriter(new file(datafile+"-pos")); printwriter negative = new printwriter(new file(datafile+"-neg")); ) { // kddcup file uses '.\n' @ end of each line // setting delimiter consume period in.usedelimiter("[.]\r\n|[.]\n|\n"); sample curr; while(in.hasnext()) { // line hold fields single sample scanner line = new scanner(in.next()); line.usedelimiter(", *"); curr = new sample(line); if (curr.getlabel().equals("normal")) { positive.println("good"); } else { negative.println("bad"); } } } } }
Comments
Post a Comment