sp; System.exit(0);
break;
default:
System.out.println("呵呵");
break;
}
}
}
//把数据从文件中读取出来 存入到集合
public static void readFromFile(ArrayList<Student> list,String fileName) throws IOException{
// 创建BR对象
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
// 9003,阿拉并,20,迪拜
// 把读取到的一行信息 切割成各个字段
String[] ss = line.split(",");
// 把散装的数组组成对象
Student s = new Student(ss[0], ss[1], ss[2], ss[3]);
// 把对象添加到集合中
list.add(s);
}
// 关闭资源
br.close();
System.out.println("初始化完毕");
}
// 把集合中的数据 写入到文件
public static void save2File(ArrayList<Student> list ,String fileName) throws IOException{
//创建BW对象
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
//遍历集合获取学生信息, 写入到文件
for (int i = 0; i < list.size(); i++) {
Student tmp = list.get(i);
// 9001,张三,18,北京
//使用sb按照指定的格式拼装学生的信息
StringBuilder sb = new StringBuilder();
sb.append(tmp.getId()).append(",")
.append(tmp.getName()).append(",")
.append(tmp.getAge()).append(",")
.append(tmp.getHome());
bw.write(sb.toString());
bw.newLine();// 换行