我正在尝试将我拥有的字典文件拆分为多个不同长度的字典,例如,如果我想将其放入长度为 2、3、4、..... n 的较小字典中,那么我可以在其中更快地搜索它们。当我说更快时,我的意思是我会知道输入长度,因此访问相应的长度字典(整体的一小部分)将意味着更快的访问。这是我当前的实现,它生成文件但不像我想要的那样写入它们。理想情况下,所有长度为 2 的单词都将写入 length2 文本文件。有人有什么建议吗?
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 i = 0; i < 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.");
}
}