public class testMain {
public static void main (String[] args) throws Exception {
FileReader fr = new FileReader("src/testMain.java" );
FileOutputStream fos = new FileOutputStream("c.txt" , false );
Map<Character, Integer> result = new HashMap<Character, Integer>();
int len = 0 ;
while ((len = fr.read()) != -1 ) {
fos.write(len);
if (len == ' ' || len == '\n' || len == '\r' || len == '\t' ) {
continue ;
}
Integer afterPut = result.get (len);
if (afterPut == null ) {
result.put((char ) len, 1 );
}else {
result.put((char ) len, afterPut + 1 );
}
}
sopResult(result, fos);
fos.close();
fr.close();
}
public static void sopResult (Map<Character, Integer> result, OutputStream os) {
Set<Entry<Character, Integer>> entrySet = result.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
String info = entry.getKey() + ":" + entry.getValue() + "次" ;
try {
os.write(info.getBytes("utf-8" ));
os.write('\n' );
} catch (IOException e) {
e.printStackTrace();
}
System.out .println(info);
}
}
}