我要做读txt档 统计字词出现的次数 依英文字母排序写入test.txt内 求解答.要求用JAVA编多谢你.唔该你好多

例如: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();

}

}

}

自己配置一下要读取文件路径,和输出文件路径即可

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-14
是要做自然语言处理吧? Stanford NLP library 很好用的。

http://nlp.stanford.edu/
第2个回答  2012-06-14
代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.* ;
public class T {
public static void write(String s,String writePath){
File file=new File(writePath);
try {
BufferedWriter ow=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
ow.write(s);
ow.flush();
ow.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String read(String readPath){
File file=new File(readPath);
String str="";
try {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while(true){
String s=br.readLine();
str=str+s+"\r\n";
if(s==null)
break;
}
br.close();
return str;

} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String [] args) {
String input =read("C://word.txt");
if(input==null){
System.out.println("文本读入错误,文本内容可能为空或文件不存在!");
return;
}

input=input.replaceAll("\\W", " ");
String[] arr = input.split("\\s+");
Set<String> set = new HashSet<String>();
for(int i=0;i<arr.length;i++){
set.add(arr[i]);
}
Map<String, Integer> map = new HashMap<String, Integer>();
Iterator<String> it = set.iterator();
while(it.hasNext()){
String s=it.next();
int count=0;
for(int i=0;i<arr.length;i++){
if(s.equals(arr[i]))
count++;
}
map.put(s, count);
}
Map<String, Integer> sortedMap = new TreeMap<String, Integer>(map);
String s=sortedMap.toString();
s=s.substring(1,s.length()-1);
String []str=s.split(",");
String[] word=new String[str.length];
String[]count=new String[str.length];
String s1="";
for(int i=0;i<str.length;i++){
word[i]=str[i].replaceAll("=", "").replaceAll("\\d", "").trim();
count[i]=str[i].replaceAll("\\D", "").trim();
if(!"".equals(word[i])){
System.out.println("单词:"+word[i]+"出现"+count[i]+"次数:");
s1=s1+word[i]+",";
// s1=s1+word[i]+":"+count[i]+"\r\r";是将单词次数也写入文本中
}
}
write(s1.substring(0, s1.length()-1),"C://newword.txt");//写入文本:C://newword.txt
}
}
将本程序代买拷到C:word.txt中运行结果如下:
单词:BufferedReader出现3次数:
单词:BufferedWriter出现3次数:
单词:C出现2次数:
单词:D出现1次数:
单词:File出现5次数:
单词:FileInputStream出现2次数:
单词:FileOutputStream出现2次数:
单词:HashMap出现1次数:
单词:HashSet出现1次数:
单词:IOException出现3次数:
单词:InputStreamReader出现2次数:
单词:Integer出现4次数:
单词:Iterator出现1次数:
单词:Map出现2次数:
单词:OutputStreamWriter出现2次数:
单词:Set出现1次数:
单词:String出现24次数:
单词:System出现3次数:
单词:T出现1次数:
单词:TreeMap出现1次数:
单词:W出现1次数:
单词:add出现1次数:
单词:args出现1次数:
单词:arr出现5次数:
单词:br出现3次数:
单词:break出现1次数:
单词:catch出现2次数:
单词:class出现1次数:
单词:close出现2次数:
单词:count出现6次数:
单词:d出现1次数:
单词:e出现4次数:
单词:else出现1次数:
单词:equals出现1次数:
单词:file出现4次数:
单词:flush出现1次数:
单词:for出现3次数:
单词:hasNext出现1次数:
单词:i出现20次数:
单词:if出现4次数:
单词:import出现9次数:
单词:input出现5次数:
单词:int出现4次数:
单词:io出现8次数:
单词:it出现3次数:
单词:iterator出现1次数:
单词:java出现9次数:
单词:length出现6次数:
单词:main出现1次数:
单词:map出现3次数:
单词:n出现2次数:
单词:new出现13次数:
单词:newword出现1次数:
单词:next出现1次数:
单词:null出现4次数:
单词:out出现3次数:
单词:ow出现4次数:
单词:package出现1次数:
单词:printStackTrace出现2次数:
单词:println出现3次数:
单词:programe出现1次数:
单词:public出现4次数:
单词:put出现1次数:
单词:r出现2次数:
单词:read出现2次数:
单词:readLine出现1次数:
单词:readPath出现2次数:
单词:replaceAll出现4次数:
单词:return出现3次数:
单词:s出现15次数:
单词:s出现16次数:
单词:set出现3次数:
单词:sortedMap出现2次数:
单词:split出现2次数:
单词:static出现3次数:
单词:str出现10次数:
单词:substring出现1次数:
单词:toString出现1次数:
单词:trim出现2次数:
单词:true出现2次数:
单词:try出现2次数:
单词:txt出现2次数:
单词:util出现1次数:
单词:void出现2次数:
单词:while出现2次数:
单词:word出现6次数:
单词:write出现3次数:
单词:writePath出现2次数:

有问题就追问,满意请采纳!
相似回答