public class Exercise3 {
// 3.将day19-笔记.txt中的所有字符按照码表值排序不去重复, 存入另一个文件中(也就是aaaabbbbbbccccc.....的方式)
public static void main(String[] args) throws IOException {
Reader in = new BufferedReader(new FileReader("day19-笔记.txt"));
Set<Character> set = new TreeSet<Character>(new MyComparator());
int b;
while((b = in.read()) != -1)
set.add((char)b);
in.close();
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("treesetday19")));
//for(Character ch : set)
for(char ch : set)
out.write(ch);
out.close();
}
}
class MyComparator implements Comparator<Character> {
public int compare(Character a, Character b) {
return a - b != 0 ? a - b : 1;
}
}
"day19-笔记.txt" 和 "treesetday19" 都是文档文件
也乱码了 对照原文件 也没看出问题 不知哪里出错了 拜求明白人指导
|
|