把对象存到集合中,再存到文件里,for循环为什么不执行??- import java.io.*;
- import java.util.*;
- //import java.lang.*;
- class person implements Comparable<person>
- {
- private String name;
- private int age;
- private int text;
- person(String name,int age,int text){
- this.name=name;
- this.age=age;
- this.text=text;
- }
- public int hashCode(){
- return name.hashCode()+age;
- }
- public boolean equals(Object obj){
- if(!(obj instanceof person))
- {
- }
- person p=(person)obj;
- return this.name.equals(p.name)&&this.age==p.age;
- }
- public int compareTo (person a){
- person s=a;
- if(this.age==s.age)
- return this.name.compareTo(s.name);
- return this.age-s.age;
-
-
- }
- public String tostring(){
- return this.name+" "+(this.age+"")+" "+(this.text+"");
- }
-
-
- }
- public class zifudemo {
- /**
- * @param args
- */
-
- public static void main(String[] args) throws IOException
- {// TODO Auto-generated method stub
- //demo();
- TreeSet<person> sss=new TreeSet<person>();
- File file=new File("E:\\javatext\\javademo3.txt");
- rinfo(sss);
- w2file(sss,file);
-
- }
- public static void rinfo(TreeSet<person> set)throws IOException
- {
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- String s=null;
- set=new TreeSet<person>();
- while(true){
- s=br.readLine();
- if(s.equals("over"))
- {
- break;
- }
- String [] st=s.split(" ");
- System.out.println("执行到了while");
- set.add(new person(st[0],Integer.parseInt(st[1]),Integer.parseInt(st[2])));
-
- }
- System.out.println("执行到了rinfo");
- br.close();
-
-
- }
- public static void w2file(Set<person> s,File f)throws IOException
- {
- BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f,true)));
-
- for(person ss:s)
- {
- bw.write(ss.tostring());
- bw.newLine();
- bw.flush();
- System.out.println("执行到for");
- }
- bw.close();
-
-
- System.out.println("执行到w2file");
-
- }
-
复制代码 |