输入字母频率java_用javaIO流读取文本中英文字母和英文单词的出现次数及频率...

packagetotal;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.util.HashMap;importjava.util.Map;public classStatistics_words {public Map map1 = new HashMap();public static void main(String arg[]) throwsIOException {

String sz[];

Integer num[];final int MAXNUM = 20;

sz= new String[MAXNUM + 1];

num= new Integer[MAXNUM + 1];

Statistics_words Statistics_words= newStatistics_words();int account = 1;//Vector ve1=new Vector();

Statistics_words.textImport();

System.out.println("文本出现单词的次数情况为:");int g_run = 0;for (g_run = 0; g_run < MAXNUM + 1; g_run++) {

account= 1;for (Map.Entryit : Statistics_words.map1.entrySet()) {if (account == 1) {

sz[g_run]=it.getKey();

num[g_run]=it.getValue();

account= 2;

}if (account == 0) {

account= 1;continue;

}if (num[g_run]

sz[g_run]=it.getKey();

num[g_run]=it.getValue();

}//System.out.println("英文单词: "+it.getKey()+" 该英文单词出现次数: "+it.getValue());

}

Statistics_words.map1.remove(sz[g_run]);

}int g_count = 1;

String tx1= newString();for (int i = 0; i < g_run; i++) {if (sz[i] == null)continue;if (sz[i].equals(""))continue;

tx1+= "出现次数第" + (g_count) + "多的单词为:" + sz[i] + "\t\t\t出现次数: " + num[i] + "\r\n";

System.out.println("出现次数第" + (g_count) + "多的单词为:" + sz[i] + "\t\t\t出现次数: " +num[i]);

g_count++;

}

Statistics_words.textExport(tx1);

}public void textImport() throwsIOException {

File a= new File("C:\\Users\\22400\\Desktop\\a.txt");

FileInputStream b= newFileInputStream(a);

InputStreamReader c= new InputStreamReader(b, "UTF-8");

String string2= newString();while(c.ready()) {char string1 = (char) c.read();if (!isWord(string1)) {if(map1.containsKey(string2)) {

Integer num1= map1.get(string2) + 1;

map1.put(string2, num1);

}else{

Integer num1= 1;

map1.put(string2, num1);

}

string2= "";

}else{

string2+=string1;

}

}if (!string2.isEmpty()) {if(map1.containsKey(string2)) {

Integer num1= map1.get(string2) + 1;

map1.put(string2, num1);

}else{

Integer num1= 1;

map1.put(string2, num1);

}

string2= "";

}

c.close();

b.close();

}public void textExport(String txt) throwsIOException {

File fi= new File("StatisticsWord.txt");

FileOutputStream fop= newFileOutputStream(fi);

OutputStreamWriter ops= new OutputStreamWriter(fop, "UTF-8");

ops.append(txt);

ops.close();

fop.close();

}public boolean isWord(chara) {if (a <= 'z' && a >= 'a' || a <= 'Z' && a >= 'A')return true;return false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值