package test1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main2 {
public static void main(String[] args) {
int count = 0;
File file = new File("F:\1.txt");
String s = "";
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
try {
while ((s = br.readLine()) != null) {
buffer.append(s + '\n');
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Pattern p = Pattern.compile("\\b[a-zA-Z]+\\b");
Matcher m = p.matcher(buffer.toString());
while(m.find()) {
System.out.println(m.group());
count ++ ;
}
System.out.println("总共:" + count + "个单词");
}
}
对什么排序,是对单词排序么?
...
Comparator<String> comparator = new Comparator<String>(){
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
};
Matcher m = p.matcher(buffer.toString());
ArrayList<String> list = new ArrayList<String>();
while(m.find()) {
list.add(m.group().toString());
count ++ ;
}
Collections.sort(list,comparator);
for(String s: list)
System.out.println(s);
}
System.out.println("总共:" + count + "个单词");
String arr[] = {"HELLO", "WORLD", "good"};
// 按ascii码顺序排序 A~Z, a-z排序
Arrays.sort(arr);
java.util.Arrays 导入这个工具包