例如:I in apple
apple 1
I 1
in 1
不是 是要编写的Java程式
1.读TXT档
2.统计英文字词出现次数
3.英文字词依英文字母排序开始排序
4.再写入新的TXT档
求解答.求用JAVA编写多谢你
月光雪松 多谢你
JAVA初学
可以执行成功
再请教如果读的TXT档内有<>\&之类的符号如何去除?
楼主怎么不自己做呢,很简单的
上源代码:
package readfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class WordsCount {
HashMap<String,Integer> hashMap;
BufferedReader infile;
String filename = "D:\\my files\\test.txt";
String string;
String outpath = "D:\\my files\\out.txt";;
@SuppressWarnings("unchecked")
public WordsCount() throws IOException{
infile = new BufferedReader(new FileReader(filename));
hashMap=new HashMap<String,Integer>();
while((string = infile.readLine()) !=null) {
String[] words=string.split(" ");
for(int i=0;i<words.length;i++){
if(words[i].trim().equals("")){
continue;
}
String astr=words[i].trim();
if(astr.endsWith(".")||astr.endsWith(",")){
astr=astr.substring(0, astr.length()-1);
}
if(hashMap.containsKey(words[i])){
Integer count=(Integer) hashMap.get(words[i]);
count++;
hashMap.remove(astr);
hashMap.put(astr, count);
}else{
hashMap.put(astr, 1);
}
}
}
infile.close();
List<String> arrayList=new ArrayList<String>();
Iterator<?> iter = hashMap.entrySet().iterator();
outer:while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String)entry.getKey();
char aChar=key.charAt(0);
for(int i=0;i<arrayList.size();i++){
if(aChar<arrayList.get(i).charAt(0)){
arrayList.add(i,key);
continue outer;
}
}
arrayList.add(key);
}
StringBuffer outContent=new StringBuffer();
for(int i=0;i<arrayList.size();i++){
String key=arrayList.get(i);
outContent.append(key+" "+hashMap.get(key)+"\r\n");
System.out.println(key+" "+hashMap.get(key));
}
FileOutputStream outs=new FileOutputStream(new File(outpath));
outs.write(outContent.toString().getBytes());
outs.flush();
outs.close();
}
public static void main(String[] args){
try {
new WordsCount();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
自己配置一下要读取文件路径,和输出文件路径即可