/*
2.给定一个list集合:{"张柏芝","刘德华","张亮","张靓颖","杨颖","黄晓明"}
1.编写一个方法:将list集合中所有姓张的人员写入到D:\\a.txt中
2.编写一个方法:将D:\\a.txt中所有姓张的人员信息读取出来并打印到控制台上
*/
public class Demo_10 {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add("张柏芝");
list.add("刘德华");
list.add("张亮");
list.add("张靓颖");
list.add("杨颖");
list.add("黄晓明");
System.out.println(list);
// 创建字符输入流对象
FileWriter writer = new FileWriter("D:\\a.txt");
BufferedWriter bw = new BufferedWriter(writer);
for (String thisstring : list) {
if (thisstring.startsWith("张")) {
bw.write(thisstring);
}
FileReader reader = new FileReader("D:\\a.txt");
BufferedReader br = new BufferedReader(reader);
String s = "";
if ((s = br.readLine()) != null) {
System.out.println(s);
}
}
bw.close();
}
}
|
|