帮忙写一个字数统计的小程序。

wo111180611 2013-10-25 10:56:26
导入一个txt文件 输出统计里面各个字出现的次数到一个txt文件中 (中文字数,英文字母数量)
我想的是用HashMap或者HahSet 希望大家帮忙写一下
...全文
531 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2013-10-28
  • 打赏
  • 举报
回复
public static void main(String[] args) throws IOException {
		Map<Integer, String> m = new TreeMap<Integer, String>();
		m.put(10, "1111");
		m.put(3, "1234");
		m.put(7, "4545");

		Entry[] d=getSortedHashtableByKey(m);
		for (Entry entry : d) {
			System.out.println(entry.getKey()+"--->"+entry.getValue());
		}
	}

	public static Map.Entry[] getSortedHashtableByKey(Map h) {
		Set set = h.entrySet();
		Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
		Arrays.sort(entries, new Comparator() {
			public int compare(Object arg0, Object arg1) {
				Object key1 = ((Map.Entry) arg0).getKey();
				Object key2 = ((Map.Entry) arg1).getKey();
				return ((Comparable) key2).compareTo(key1);
			}
		});

		return entries;
	}
tony4geek 2013-10-28
  • 打赏
  • 举报
回复
map 排序网上很多
尘缘udbwcso 2013-10-27
  • 打赏
  • 举报
回复
文件有多大,文件太大的话就要考虑空间和效率了
wo111180611 2013-10-27
  • 打赏
  • 举报
回复
引用 1 楼 a470577391 的回复:
package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Count {
	
	public void count(File file){
		//存储字符的Map
		Map<Character,Integer> map = new HashMap<Character,Integer>();
		//输入输出流
		BufferedReader reader = null;
		PrintWriter writer = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			writer = new PrintWriter(new FileWriter(new File("E:\\count.txt"),true));//true表明可以在文件末尾追加
			
			String line = null;	
			while( (line=reader.readLine())!=null ){		//每次读取一行
				char[] l = line.toCharArray();				//转为char数组遍历
				
				for(int i=0;i<l.length;i++){
					if(l[i]==' '||l[i]=='\n'||l[i]=='\t'){	//不计算空格等
						
					}else if( map.get(l[i])==null ){		//为null说明是第一次出现
						map.put(l[i], 1);
					}else{									//在原来的基础上加1
						map.put(l[i],  map.get(l[i])+1);
					}
				}
			}
			
			Iterator<Character> it = map.keySet().iterator();		//用迭代器遍历Map
			while(it.hasNext()){
				char key = it.next();								//得到key
				writer.append(key + "-->" + map.get(key));			//存储到文件
				writer.println();									//换行
				writer.flush();
			}
			
		} catch (Exception e) {
			System.out.println("出现异常,未能正常统计");
		} finally{
			try {
				reader.close();		//关闭流			
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("字数统计完毕");
	}
	
	public static void main(String[] args) throws Exception {
		Count count = new Count();
		count.count(new File("E:\\test.txt"));
	}

}
看不合不合要求,只是实现了,可能性能不怎么好
您看加一个排序应该怎么做 就是按照出现频率的降序输出
尘缘udbwcso 2013-10-27
  • 打赏
  • 举报
回复
引用 5 楼 wo111180611 的回复:
[quote=引用 4 楼 udbwcso 的回复:] 文件有多大,文件太大的话就要考虑空间和效率了
不用考虑那么多 实现功能就行[/quote] 不用考虑那么多的话就没有什么难度了 很简单的自己动手写吧
wo111180611 2013-10-27
  • 打赏
  • 举报
回复
引用 4 楼 udbwcso 的回复:
文件有多大,文件太大的话就要考虑空间和效率了
不用考虑那么多 实现功能就行
白开水MD5 2013-10-26
  • 打赏
  • 举报
回复
package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Count {
	
	public void count(File file){
		//存储字符的Map
		Map<Character,Integer> map = new HashMap<Character,Integer>();
		//输入输出流
		BufferedReader reader = null;
		PrintWriter writer = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			writer = new PrintWriter(new FileWriter(new File("E:\\count.txt"),true));//true表明可以在文件末尾追加
			
			String line = null;	
			while( (line=reader.readLine())!=null ){		//每次读取一行
				char[] l = line.toCharArray();				//转为char数组遍历
				
				for(int i=0;i<l.length;i++){
					if(l[i]==' '||l[i]=='\n'||l[i]=='\t'){	//不计算空格等
						
					}else if( map.get(l[i])==null ){		//为null说明是第一次出现
						map.put(l[i], 1);
					}else{									//在原来的基础上加1
						map.put(l[i],  map.get(l[i])+1);
					}
				}
			}
			
			Iterator<Character> it = map.keySet().iterator();		//用迭代器遍历Map
			while(it.hasNext()){
				char key = it.next();								//得到key
				writer.append(key + "-->" + map.get(key));			//存储到文件
				writer.println();									//换行
				writer.flush();
			}
			
		} catch (Exception e) {
			System.out.println("出现异常,未能正常统计");
		} finally{
			try {
				reader.close();		//关闭流			
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("字数统计完毕");
	}
	
	public static void main(String[] args) throws Exception {
		Count count = new Count();
		count.count(new File("E:\\test.txt"));
	}

}
看不合不合要求,只是实现了,可能性能不怎么好
  • 打赏
  • 举报
回复
毛爷爷说:自己动手,丰衣足食。。。
失落夏天 2013-10-26
  • 打赏
  • 举报
回复
直说一个定义map的类型吧,其它的百度搜搜都能搜出来 Map<char,Integer> map=new HashMap<char,Integer>();

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧