csv - Multiple Over writes using FileWriter in Java -
im trying read n different csv files containing stock price data. want extract 1 particular column each file , showcase columns in single csv file.
the issue combined file contains written data first file give input i.e. data not being overwritten in iteration of loop.
can help? or suggest new method?
public static void main(string[] args) throws ioexception { int filecount=0; system.out.println("enter number of files"); scanner stream =new scanner(new inputstreamreader(system.in)); filecount= integer.parseint(stream.next()); file file2 = new file("combined_sym.csv"); filewriter fwriter= new filewriter("combined_sym.csv",true); printwriter outputfile= new printwriter(fwriter); int i; for(i=0;i<filecount;i++) { system.out.println("enter file name "+i); string filename =stream.next(); file file = new file(filename); scanner inputstream = new scanner(file); scanner inputstream2= new scanner(file2); if(!inputstream2.hasnext()){ outputfile.println(filename); } else { string header=inputstream2.next(); system.out.println(header+","+filename); outputfile.println(header+","+filename); } while(inputstream.hasnext()) { string data= inputstream.next(); string[] values = new string[8]; values = data.split(","); string sym=values[7]; if(!inputstream2.hasnext()) outputfile.println(sym); else { string data2= inputstream2.next(); outputfile.println(data2+","+sym); system.out.println(data2+","+sym); } } inputstream.close(); inputstream2.close(); outputfile.close(); } }
}
can try changing :
for(i=0;i<filecount;i++) { system.out.println("enter file name "+i); string filename =stream.next(); file file = new file(filename);
to
file file; for(i=0;i<filecount;i++) { system.out.println("enter file name "+i); string filename =stream.next(); file = new file(filename);
Comments
Post a Comment