/** * 题目:题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 * 时间:2015年7月28日10:04:33 * 文件:lianxi07.java * 作者:cutter_point */ package bishi.zuixin50.t2015728; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Lianxi07 { public static void main(String[] args) { //统计所有的英文字母,也就是ascii码 String path = "source/zuixin50/lianxi07input.txt"; String path2 = "source/zuixin50/lianxi07output.txt"; List lis = Lianxi07.getLines(path); //获取文本中所有的字符 Map map = Lianxi07.count(lis); //吧统计的数据输出到文件中 Lianxi07.out(map, path2); } public static void out(Map map, String path) { //我们遍历map,吧数据输出到文件中 FileOutputStream fos = null; try { fos = new FileOutputStream(new File(path)); //遍历map for(Object o : map.entrySet()) { Map.Entry<String, Integer> entry = (Entry<String, Integer>) o; //其实这里可以直接输出:out = entry.getKey() + "的个数是:" + entry.getValue() + "\n"; //这样写就不用进行判断了 if(entry.getKey() == "zimu") { String out = "字母的个数是:" + entry.getValue() + "\n"; System.out.println(out); fos.write(out.getBytes()); } else if(entry.getKey() == "kongge") { String out = "空格的个数是:" + entry.getValue() + "\n"; System.out.println(out); fos.write(out.getBytes()); } else if(entry.getKey() == "shuzi") { String out = "数字的个数是:" + entry.getValue() + "\n"; System.out.println(out); fos.write(out.getBytes()); } else { String out = "其他的个数是:" + entry.getValue() + "\n"; System.out.println(out); fos.write(out.getBytes()); } } } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (Exception e2) { e2.printStackTrace(); } } } /** * 我们统计这一行数据中的所有的字母,空格、数字和其他字符 * @param lines 一行字符串 * @return */ public static Map count(List lines) { //这里我们用一个map来存放相应的数据,key是我们要统计的属性:字母,空格。。。value是值个数 Map tongji = new HashMap<String, Integer>(); //初始化map tongji.put("zimu", 0); tongji.put("kongge", 0); tongji.put("shuzi", 0); tongji.put("other", 0); //其他 //首先我们遍历所有的行 Iterator it = lines.iterator(); while(it.hasNext()) { //一行一行的取值 char[] all = ((String) it.next()).toCharArray(); //我们遍历这个字符数组 for(int i = 0; i < all.length; ++i) { if((all[i] >= 'a' && all[i] <= 'z') || (all[i] >= 'A' && all[i] <= 'Z')) { int value = (int) tongji.get("zimu"); //我们把数据+1 tongji.put("zimu", ++value); } else if((all[i] >= '0' && all[i] <= '9')) { //我们统计数字的个数 int value = (int) tongji.get("shuzi"); //我们把数据+1 tongji.put("shuzi", ++value); } else if(all[i] == ' ') { //如果是空格的话 int value = (int) tongji.get("kongge"); //我们把数据+1 tongji.put("kongge", ++value); } else { //其他 int value = (int) tongji.get("other"); //我们把数据+1 tongji.put("other", ++value); } }//for }//while //统计完毕 return tongji; } public static String getLine(String path) { //从文件中获取一行数据 String line = null; //一个文件输入流 InputStreamReader isr = null; //用来读取一行 BufferedReader br = null; try { isr = new InputStreamReader(new FileInputStream(new File(path))); br = new BufferedReader(isr); line = br.readLine(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { //关闭IO资源 try { br.close(); isr.close(); } catch (Exception e) { e.printStackTrace(); } } return line; } //读取文件的内容保存到String的数组中 public static List getLines(String path) { //从文件中获取一行数据 String line = null; List<String> lines = new ArrayList(); //一个文件输入流 InputStreamReader isr = null; //用来读取一行 BufferedReader br = null; try { isr = new InputStreamReader(new FileInputStream(new File(path))); br = new BufferedReader(isr); while( (line = br.readLine()) != null) { //吧读取到的一行数据存放到数组中 lines.add(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { //关闭IO资源 br.close(); isr.close(); } catch (Exception e) { e.printStackTrace(); } } return lines; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/cutter_point/article/details/47362367