我想分割一個字典文件,我有不同長度的多個字典,例如,如果我想把它放到長度爲2,3,4的小字典中,..... n,我可以在那裏更快地搜索它們。當我說得更快時,我的意思是我會知道輸入長度,因此訪問相應的長度字典(一小部分)將意味着更快的訪問。這是我目前的實現,它可以生成文件,但不會像我所希望的那樣寫入它們。理想情況下,所有長度爲2的單詞都將寫入length2文本文件。任何人有任何建議?試圖一次寫入多個文件 - Java
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.");
}
}
我正在製作一個解決雜亂的程序,就像在報紙上一樣。因此,二分法搜索一個只是字典大小的一小部分而不是整個事物的列表會更快。比如如果有七個混亂的字符,我只搜索字典中包含七個字母的單詞。 – dsiebert424 2013-05-08 17:56:57
_「確實[不?]像我想要的那樣寫給他們」 - 這非常含糊。你想要什麼行爲?實際行爲有什麼不同? – 2013-05-08 17:57:02
因此,如果單詞是「蘋果」,例如,它會看到它是長度爲五,並將其寫入相應的長度爲5的文本文件。但是,當我運行這個時,它只會產生一堆空文件。 – dsiebert424 2013-05-08 18:01:38