读取文件,拿到首字母大写的单词,存入集合
public class test2 extends ArrayList {//继承ArrayList,用他父类的构造方法
public static void main(String[] args) throws IOException {
//使用字符流读取一个文件,
reader("G:\\\\Demo\\\\practice\\\\test2.txt");
TreeSet<String> treeSet=new TreeSet<>(
new test2("G:\\Demo\\practice\\test2.txt","\\W+")
);
System.out.println(treeSet.headSet("a"));
}
public static String reader(String fileName) throws IOException {
//使用字符流读取一个文件,
StringBuilder stringBuilder =new StringBuilder();
//字符输入流包装成缓冲流
BufferedReader bf=null;
try {
bf=new BufferedReader(new FileReader(
//得到文件的绝对路径
new File(fileName).getAbsoluteFile())
);
String s;
while((s=bf.readLine())!= null){
stringBuilder.append(s);
stringBuilder.append("\n");
System.out.println(s);
//将首字母大写的单词放入集合,然后按照26个英文字母的顺序排序
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
bf.close();
}
return stringBuilder.toString();
}
//构造
public test2(String fileName,String splite) throws IOException {
super(Arrays.asList(reader(fileName).split(splite)));//调用ArrayList的父类构造,需要一个数组
if (get(0).equals("")){
remove(0);
}
}
}
读取文件 拿到首字母大写单词存入集合