java 读取文件中每个单词出现的次数
packagea;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.Data...
package a;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordProbability {
File sourceFile=new File("/Users/liu/Documents/news.rtf");
public void count() throws IOException{
FileInputStream fin=new FileInputStream(sourceFile);
BufferedReader br=new BufferedReader(new InputStreamReader(fin));
String str=br.readLine();
String[] s=str.split(" ");
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
for (int i = 0; i < s.length; i++) {
String key = s[i].toLowerCase();
if (s[i].length() > 1) {
if (map.get(key) == null) {
map.put(key, 1);
}
else {
int value = map.get(key).intValue();
value++;
map.put(key, value);
}
}
}
}
}
输出map为啥是乱码55555555555555555
5555555555555高手在哪里咩 展开
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordProbability {
File sourceFile=new File("/Users/liu/Documents/news.rtf");
public void count() throws IOException{
FileInputStream fin=new FileInputStream(sourceFile);
BufferedReader br=new BufferedReader(new InputStreamReader(fin));
String str=br.readLine();
String[] s=str.split(" ");
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
for (int i = 0; i < s.length; i++) {
String key = s[i].toLowerCase();
if (s[i].length() > 1) {
if (map.get(key) == null) {
map.put(key, 1);
}
else {
int value = map.get(key).intValue();
value++;
map.put(key, value);
}
}
}
}
}
输出map为啥是乱码55555555555555555
5555555555555高手在哪里咩 展开
6个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询