[Java] 纯文本查看 复制代码
public class Test {
public static void main(String[] args) throws IOException {
//创建一个ArrayList集合
ArrayList<String> list = new ArrayList<>();
list.add("张柏芝");
list.add("刘德华");
list.add("张亮");
list.add("张靓颖");
list.add("杨颖");
list.add("黄晓明");
list.add("张柏芝");
list.add("刘德华");
//创建一个HashSet集合
HashSet<String> hs = new HashSet<>();
hs.addAll(list);
//清空list
list.clear();
//将元素添加到list中
list.addAll(hs);
//创建输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
for(String s : list) {
bw.write(s);
bw.newLine();
bw.flush();
}
//关闭流
bw.close();
//创建输入流对象
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String line;
while((line = br.readLine())!=null) {
if(line.startsWith("张"))
System.out.println(line);
}
}
}