标签:bre generate turn imp code sys 集合 equal finally
package scanner; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import java.util.TreeSet; public class Demo { public static void main(String[] args) { //定义学生的集合 TreeSet<Student> ts = new TreeSet<Student>(); //引入Scanner对象 Scanner sc = new Scanner(System.in); System.out.println("请输入学生的数量"); int count = sc.nextInt(); for(int i=0;i<count;i++) { sc = new Scanner(System.in); System.out.println("请输入学生的姓名"); //学生的姓名 String name = sc.nextLine(); //学生的年龄 System.out.println("请输入学生的年龄"); Integer age = sc.nextInt(); System.out.println("请输入学生的ID"); Integer id = sc.nextInt(); Student s = new Student(); s.setId(id); s.setAge(age); s.setName(name); ts.add(s); } BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter("f.txt")); for(Student s:ts) { bw.write(s.getName()+"---------"+s.getAge()+"---------"+s.getId()); bw.newLine(); } bw.flush(); }catch(IOException e) { e.printStackTrace(); }finally { if(bw != null) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } System.out.println("ojbk"); } }
package scanner; public class Student implements Comparable<Student>{ private Integer Id; private String name; private Integer age; public Integer getId() { return Id; } public void setId(Integer id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public int compareTo(Student o) { int val = this.Id-o.getId(); if(val==0) { val = this.age - o.getAge(); if(val==0) { val = this.name.compareTo(o.getName()); } } return val; } }
package scanner1; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { System.out.println("请输入"); Scanner sc = new Scanner(System.in); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter("g.txt")); String line = null; while((line = sc.nextLine()) != null ) { if("exit".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(bw != null) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
package scanner1; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class ScannerDemo2 { public static void main(String[] args) { System.out.println("请输入"); Scanner sc = new Scanner(System.in); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter("a.txt")); String line = null; while((line = sc.nextLine())!=null) { if("exit".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(bw != null) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
标签:bre generate turn imp code sys 集合 equal finally
原文地址:https://www.cnblogs.com/java-jiangtao-home/p/9374949.html