* 统计一个文本文件中字符出现的次数,结果存入另外的一个文本文件中。
*/
public class Text5 {
public static void main(String[] args) throws IOException {
FileOutputStream out=new FileOutputStream("第五题需要被统计的文本文档.txt");
String str="fwegwegwsvsdbregwfasfqarwrqojwrgh";
byte[] b=str.getBytes();
out.write(b);
out.flush();
FileInputStream in=new FileInputStream("第五题需要被统计的文本文档.txt");
int l;
while((l=in.read(b))!=-1)
{
String se=new String(b,0,l);
char [] ch=se.toCharArray();
HashMap<Character,Integer> tree=new HashMap<Character,Integer>();
for(int x=0;x<ch.length;x++)
{
Integer value=tree.get(ch[x]); //建立键与值的关系
if(value==null)
tree.put(ch[x], 1);
else
tree.put(ch[x], value+1);
}
BufferedWriter out1=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("第五题存放结果文本.txt")));
Set<Character> set=tree.keySet();
Iterator<Character> it=set.iterator();
while(it.hasNext())
{
char temp=it.next();
out1.write(temp+" :"+tree.get(temp));
out1.newLine();
out1.flush();
}
}
}
}
|
|