【发布时间】:2011-11-27 01:38:15
【问题描述】:
我目前正在处理20个txt文件,任务是统计每个单词的词频,然后将结果输出到单个txt文件中...
例如:word --"news" 在 20 个文件中出现 47 次。 目前,我只设法将所有 20 个文件读入我的程序(我将所有文件数据存储到一个文件中——(String docBus),但我需要帮助从(String docBus)中提取单词(逐字)到一个字符串数组...顺便说一句,文件包含标点符号、数字...等...但我需要的只是计算单词频率...所以我需要避免在我的程序中使用那些标点符号、数字... 到目前为止,这是我的代码:
public class Count extends javax.swing.JFrame {
ArrayList<String> fileBusName = new ArrayList<String>();
String docBus = "";
private void returnBusFilenName(){
String str = "";
for(int i = 1; i <= 20; i++){
str = "nlg/bus" + i + ".txt";
fileBusName.add(str);
}
}
private String getFile(String file){
String strLine = "", str = "";
try{
BufferedReader in = new BufferedReader(new FileReader(file));
while((strLine = in.readLine()) != null){
str += strLine + "\n ";
}
in.close();
}catch(Exception e){
}
return str;
}
private void getDocBus(){
returnBusFilenName();
for(int i=0; i<=19; i++){
docBus = docBus + getFile(fileBusName.get(i));
}
}
【问题讨论】:
标签: java string file count word