Java: write to multiple files using a BufferedWriter array (or another way?) -
instead of having 15 bufferedwriters, there way write 15 different files using array?
public static void main (string [] args) { try { bufferedreader br = new bufferedreader(new filereader("" )); bufferedwriter bw = new bufferedwriter(new filewriter( "1occurrence.txt")); hashmap<string,integer> hash = new hashmap<string,integer>(); while ((line= br.readline() ) != null) { if ( !hash.containskey(genesym)) { hash.put (genesym, 1); int count=0; bw.write(line + "\n"); } else { count = hash.get(genesym); if (count==1) { bw2.write(line + "\n"); count++; } else if (count==2) { bw3.write(line + "\n"); count++; }
then have 12 more 'else if' statements write 15 different files.
bufferedwriter [] bw = new bufferedwriter[15]; (int i=0; i<15; i++) { array[i] = new bufferedwriter(); }
i realize syntax highly incorrect, best way can explain it.
thanks help!
i not understand why there need create array of buffered writers..
if requirement write 15 different fiels.. can have array of 15 file names , change reference of buffered writer in each iteration..
please have @ code below...
string[] filenames = {"file1.txt","file2.txt",....} // 15 file names; bufferedwriter writer = null; for(int i=0;i<filenames.length;i++) { writer = new bufferedwriter(new filewriter(filenames[i])); writer.wrtier(data); // write data file }
hope answers question.
please let me know if need further information.
Comments
Post a Comment