Tangental “answer” follows. (This should also print out contents to the files, unless I’m missing something very basic.)
Whenever there is a set of variables in the form xN
(e.g. l2
, l3
, l22
), they should usually be replaced with a List collection type such as an ArrayList.
This is just an example to show how can be reduce duplication and fixed bounds:
int MAX_WORD_LEN = 22; // making this dynamic left as an excercise
List<PrintWriter> writers = new ArrayList<PrintWriter>();
for (int i = 0; i <= MAX_WORD_LEN; i++) {
PrintWriter w = new PrintWriter("dictionary_length" + i + ".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);
}
}
for (PrintWriter w : writers) {
w.close();
}
Slight adjustments can be made to not create a “0” or “1” file.
4
solved Trying to Write Multiple Files at Once – Java