Trying to Write Multiple Files at Once - Java -
i trying split dictionary file have multiple dictionaries of different lengths, example if want take , put smaller dictionaries of length 2, 3, 4, ..... n, can search them quicker. when quicker mean know input length , therefore accessing corresponding length dictionary (a fraction of whole) mean quicker accesses. current implementation generates files doesn't write them desire. ideally, words of length 2 example written length2 text file. have suggestions?
import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.printwriter; import java.io.ioexception; public class main { public static void main(string[] args) throws ioexception { filereader fr = new filereader("dictionary.txt"); printwriter l2 = new printwriter("dictionary_length2.txt", "utf-8"); printwriter l3 = new printwriter("dictionary_length3.txt", "utf-8"); printwriter l4 = new printwriter("dictionary_length4.txt", "utf-8"); printwriter l5 = new printwriter("dictionary_length5.txt", "utf-8"); printwriter l6 = new printwriter("dictionary_length6.txt", "utf-8"); printwriter l7 = new printwriter("dictionary_length7.txt", "utf-8"); printwriter l8 = new printwriter("dictionary_length8.txt", "utf-8"); printwriter l9 = new printwriter("dictionary_length9.txt", "utf-8"); printwriter l10 = new printwriter("dictionary_length10.txt", "utf-8"); printwriter l11 = new printwriter("dictionary_lengty11.txt", "utf-8"); printwriter l12 = new printwriter("dictionary_length12.txt", "utf-8"); printwriter l13 = new printwriter("dictionary_length13.txt", "utf-8"); printwriter l14 = new printwriter("dictionary_length14.txt", "utf-8"); printwriter l15 = new printwriter("dictionary_length15.txt", "utf-8"); printwriter l16 = new printwriter("dictionary_length16.txt", "utf-8"); printwriter l17 = new printwriter("dictionary_length17.txt", "utf-8"); printwriter l18 = new printwriter("dictionary_length18.txt", "utf-8"); printwriter l19 = new printwriter("dictionary_length19.txt", "utf-8"); printwriter l20 = new printwriter("dictionary_length20.txt", "utf-8"); printwriter l21 = new printwriter("dictionary_length21.txt", "utf-8"); bufferedreader tr = new bufferedreader(fr); string temp; int temp_length; for(int = 0; < 60388; i++){ temp = new string(tr.readline()); temp_length = temp.length(); if(temp_length == 2) l2.println(temp); if(temp_length == 3) l3.println(temp); if(temp_length == 4) l4.println(temp); if(temp_length == 5) l5.println(temp); if(temp_length == 6) l6.println(temp); if(temp_length == 7) l7.println(temp); if(temp_length == 8) l8.println(temp); if(temp_length == 9) l9.println(temp); if(temp_length == 10) l10.println(temp); if(temp_length == 11) l11.println(temp); if(temp_length == 12) l12.println(temp); if(temp_length == 13) l13.println(temp); if(temp_length == 14) l14.println(temp); if(temp_length == 15) l15.println(temp); if(temp_length == 16) l16.println(temp); if(temp_length == 17) l17.println(temp); if(temp_length == 18) l18.println(temp); if(temp_length == 19) l19.println(temp); if(temp_length == 20) l20.println(temp); if(temp_length == 21) l21.println(temp); } tr.close(); l2.close(); l3.close(); l4.close(); l5.close(); l6.close(); l7.close(); l8.close(); l9.close(); l10.close(); l11.close(); l12.close(); l13.close(); l14.close(); l15.close(); l16.close(); l17.close(); l18.close(); l19.close(); l20.close(); l21.close(); system.out.println("complete."); } }
tangental "answer" follows. (this should print out contents files, unless i'm missing basic.)
whenever there set of variables in form xn
(e.g. l2
, l3
, l22
), should usually replaced list collection type such arraylist.
this example show how can reduce duplication , fixed bounds:
int max_word_len = 22; // making dynamic left excercise list<printwriter> writers = new arraylist<printwriter>(); (int = 0; <= max_word_len; i++) { printwriter w = new printwriter("dictionary_length" + + ".txt", "utf-8"); writers.add(w); } string line; while ((line = tr.readline()) != null) { int len = line.length(); if (len < writers.size()) { writers.get(len).println(line); } } (printwriter w : writers) { w.close(); }
slight adjustments can made not create "0" or "1" file.
Comments
Post a Comment