后续
System.out.println("姓名:"); String name = sc.nextLine(); System.out.println("语文成绩:"); int chinese = sc.nextInt(); System.out.println("数学成绩:"); int math = sc.nextInt(); System.out.println("英语成绩:"); int english = sc.nextInt(); //创建学生对象,把键盘录入的数据添加到对象中 Student s = new Student();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnglish(english); //把学生对象添加到TreeSet集合中
ts.add(s); } //创建字符缓冲流输出对象 BufferedWriter bw = new BufferedWriter(new FileWriter("ts.txt")); //遍历集合,得到每一个学生对象 for(Student s :ts){ //拼接学生对象 StringBuilder sb = new StringBuilder(); sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.setnum()); //调用字符缓冲流方法写对象 bw.write(sb.toString()); bw.newLine(); bw.flush(); } bw.close(); }
}