我已计算出单词,现在在控制台中显示它。但我想将控制台输出写入文本文件。怎么做?
String[] wrds = counter.getWords(WordCounter.SortOrder.BY_FREQUENCY);
int[] frequency = counter.getFrequencies(WordCounter.SortOrder.BY_FREQUENCY);
int n = counter.getEntryCount();
for (int i=0; i<n; i++)
{
This should be written in text file///// System.out.println(frequency[i] + " " + wrds[i]+" "+ counter.getWordCount());
答案 0 :(得分:2)
String[] wrds = counter.getWords(WordCounter.SortOrder.BY_FREQUENCY);
int[] frequency = counter.getFrequencies(WordCounter.SortOrder.BY_FREQUENCY);
String texttoWrite = "";
int n = counter.getEntryCount();
for (int i=0; i<n; i++)
{
texttoWrite += frequency[i] + " " + wrds[i]+" "+ counter.getWordCount();
}
try{
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(texttoWrite);
out.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
答案 1 :(得分:0)
这样的事情怎么样?
FileWriter fstream = new FileWriter(fileName);
BufferedWriter fbw = new BufferedWriter(fstream);
for (int i=0; i<n; i++) {
fbw.write(frequency[i] + " " + wrds[i]+" "+ counter.getWordCount()+ "\n");
}
答案 2 :(得分:0)
FileWriter fw = new FileWriter("path location");
fw.write(frequency[i]....);
fw.close();
答案 3 :(得分:0)
使用文件对象打开文件。并写入文件而不是控制台(或两者都写)。关闭文件。什么是真正的问题?