- 论坛徽章:
- 0
|
谢谢回复
我的情况和那个还不太一样,
没有那个复杂
只要统计出现的字符,而且不需要对相同字符计数
功能有点像sort -u
贴了一段代码
下面 //1到//2的代码在java的API里有没有对应的函数,
或者用更短的代码实现
- import java.io.CharArrayWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.Arrays;
- public class StatOccurence
- {
- static byte[] readFile(String name) throws Exception
- {
- File f = new File(name);
- byte buffer[] = new byte[(int)f.length()];
- FileInputStream s = new FileInputStream(f);
- s.read(buffer);
- return buffer;
- }
- public static void main(String[] args) throws Exception
- {
- // process with args
- char ciu[] = new String(readFile(args[1]), "utf-8").toCharArray();
- Arrays.sort(ciu);
- //1
- CharArrayWriter sortedStatSet = new CharArrayWriter();
- int i = 0;
- int total = 0;
- char c, lastc = '\ufeff';
- while(i < ciu.length)
- {
- c = ciu[i++];
- if (c == lastc || c < 0x100)
- {
- continue;
- }
- sortedStatSet.write(c);
- lastc = c;
- total++;
- }
- //2
- // do something with sortedStatSet.toCharArray()
- }
- }
复制代码 |
|