阅读背景:

统计英文文章的单词出现次数

来源:互联网 
 
 
 
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class Test {
	
	public static void main(String[] args) {
		
		String tempStr = "";
		int tempbyte = 0;
		Map<String, Integer> map = new HashMap<String, Integer>();
		BufferedReader in;
		String charSet = "utf-8";
		String filePath = "c:\file.txt";
		
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), charSet));
		}
		catch (UnsupportedEncodingException e) {
			System.out.println("不支持的字符编码:" + charSet);
			return;
		}
		catch (FileNotFoundException e) {
			System.out.println("找不到文件:" + filePath);
			return;
		}
		
		try {
			while ((tempbyte = in.read()) != -1) {
				 
				 if ((tempbyte >= 65 && tempbyte <= 90) || (tempbyte >= 97 && tempbyte <= 122) || (tempbyte == (int)'’')) {
					 tempStr += (char)tempbyte;
				 }
				 else if (!tempStr.equals("")) {
					 
					 if (map.get(tempStr) == null) {
						 map.put(tempStr, 1);
					 }
					 else {
						 map.put(tempStr, map.get(tempStr) + 1);
					 }
					 tempStr = "";
				 }
			 }
		}
		catch (IOException e) {
			System.out.println("读取文件失败");
			return;
		}
		 
		 for (Map.Entry<String, Integer> entry : map.entrySet()) {
			 System.out.println(entry.getKey() + " : " + entry.getValue());
		 }
	}
}import java.io.BufferedReader;
import jav



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: