版权声明:未经博主同意,禁止转载,联系方式qq2928013321 https://blog.csdn.net/weixin_42130471/article/details/83151991
package jihe;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import listtomap.Student;
public class collection {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Student> set = new TreeSet<Student>();
Scanner in=new Scanner(System.in);
while(true) {
System.out.println("请输入信息:");
String str=in.next();
if("exit".equals(str)) {
break;
}
String [] arr=str.split("#");
int sno=Integer.parseInt(arr[0]);
String name=arr[1];
int age=Integer.parseInt(arr[2]);
Student stu=new Student(sno,name,age);
set.add(stu);
}
Writer w;
PrintWriter pw=null;
try {
pw=new PrintWriter(new FileWriter("e:/abcd"));
for(Student stu:set) {
String str=stu.getSno()+" "+stu.getName()+" "+stu.getAge();
pw.println(str);//一行一行往文件中写,和pw.writer()不一样
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
pw.close();
}
}
}