2

我已经完成了文件的读取和写入,但是我遇到了问题。我不知道为什么它只显示文件的最后一行。在从 Person.txt 读取行的部分中,当我退出 while 循环时,我想显示每一行的 p.getName(),它只显示最后一行。我怎样才能解决这个问题?

这是我的代码:

import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class ListaHobby {

 String line="";
 Hobby h = new Hobby();
 Persoana p = new Persoana();
 BufferedWriter bw = null;
 ArrayList<Persoana> listOfPersons;
 ArrayList<Hobby> listOfHobbies;
  public void writeListaHobbies(){
    try{
      listOfPersons = new ArrayList<Persoana>();
      FileReader  file1 =new FileReader("Persoane.txt");


      listOfHobbies = new ArrayList<Hobby>();
      FileReader file2 = new FileReader("Hobby.txt");


      BufferedReader br1 = new BufferedReader(file1);
      BufferedReader br2 = new BufferedReader(file2);

      while((line1 = br1.readLine()) != null){

         if(!line1.trim().contains("ID")){
          String[] attributes = line1.split(";");// split it at every ";"

            //Person person = new Person();           // make a new person
            p.setNume(attributes[1]);
            p.setPrenume(attributes[2]);
            p.setDataNasterii(attributes[3]);
            p.setProfesie(attributes[4]);


             listOfPersons.add(p);
         }
      }

     System.out.println(p.getNume());
      while((line2 = br2.readLine()) != null){


          if(!line2.trim().contains("ID")){
            String[] attributes = line2.split(";");  // split it at every ";"

                      // make a new person
            h.setNume(attributes[1]);
            h.setDescriere(attributes[2]);
            h.setNrPers(attributes[3]);
            h.setElemNecesar(attributes[4]);

            listOfHobbies.add(h);

          }

      }
     System.out.println(h.getNume());

      FileWriter fw = new FileWriter("PersHobby.txt");
      bw = new BufferedWriter(fw);

     for(int i = 0;i < listOfPersons.size(); i++) {

        //for(int j = 0 ; j < listOfHobbies.size();j++) {

             if((p.getId())== (h.getId()))
                 p.addHobby(h);

             String s = p.getNume() + " " +p.getPrenume() +
                     ": " + h.getNume() + ", " + h.getNume();
             System.out.println(s);
             bw.write(s);
             bw.newLine();
     }
      bw.close();
    }
    catch(IOException ex){
        System.out.println("Error opening file.");
        System.exit(1);
    }
  }
}
4

3 回答 3

0

您需要在读取输入文件的每一行时添加人员和爱好,并在写入文件时遍历人员和爱好。

import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class ListaHobby {

 String line="";
 Hobby h;
 Persoana p;
 BufferedWriter bw = null;
 ArrayList<Persoana> listOfPersons;
 ArrayList<Hobby> listOfHobbies;
  public void writeListaHobbies(){
    try{
      listOfPersons = new ArrayList<Persoana>();
      FileReader  file1 =new FileReader("Persoane.txt");


      listOfHobbies = new ArrayList<Hobby>();
      FileReader file2 = new FileReader("Hobby.txt");


      BufferedReader br1 = new BufferedReader(file1);
      BufferedReader br2 = new BufferedReader(file2);

      while((line1 = br1.readLine()) != null){

         if(!line1.trim().contains("ID")){
          String[] attributes = line1.split(";");// split it at every ";"

            p = new Persoana();           // make a new person
            p.setNume(attributes[1]);
            p.setPrenume(attributes[2]);
            p.setDataNasterii(attributes[3]);
            p.setProfesie(attributes[4]);


             listOfPersons.add(p);
         }
      }

     System.out.println(p.getNume());
      while((line2 = br2.readLine()) != null){


          if(!line2.trim().contains("ID")){
            String[] attributes = line2.split(";");  // split it at every ";"

            h=new Hobby();
            h.setNume(attributes[1]);
            h.setDescriere(attributes[2]);
            h.setNrPers(attributes[3]);
            h.setElemNecesar(attributes[4]);

            listOfHobbies.add(h);

          }

      }
     System.out.println(h.getNume());

      FileWriter fw = new FileWriter("PersHobby.txt");
      bw = new BufferedWriter(fw);

     for(int i = 0;i < listOfPersons.size(); i++) {
        p=listOfPersons.get(i);
        for(int j = 0 ; j < listOfHobbies.size();j++) {
             h=listOfHobbies.get(j);
             if(p.getId())== h.getId()))
                 p.addHobby(h);

             String s = p.getNume() + " " +p.getPrenume() +
                     ": " + h.getNume() + ", " + h.getNume();
             System.out.println(s);
             bw.write(s);
             bw.newLine();
         }
     }
      bw.close();
    }
    catch(IOException ex){
        System.out.println("Error opening file.");
        System.exit(1);
    }
  }
}
于 2012-05-02T15:57:21.987 回答
0

您正在使用 和 的单个实例Person添加Hobby到各自的列表中。new Person相反,对于找到的每个(唯一?)ID ,创建new Hobby并使用它们添加到列表中。

p = new Person();  
p.setXXX...  
...   

爱好也是如此。

h = new Hobby();
h.setXXX...   
...

除非您这样做,否则您只会将最新发现的数据添加到相同的数据中,person并将hobby对象添加到列表中。

于 2012-05-02T15:52:59.130 回答
0

您不断将同一个人添加到您的列表中。您需要通过取消注释行来在每次迭代中创建一个新对象。完成此操作后,您可以删除类成员 (p),因为不再需要它,因为您将人员存储在 ArrayList 中。

同样的评论适用于爱好列表。

  while((line1 = br1.readLine()) != null){

     if(!line1.trim().contains("ID")){
      String[] attributes = line1.split(";");// split it at every ";"

        Persoana p = new Persoana();  // <~~~ you need to create a new person for each line
        p.setNume(attributes[1]);
        p.setPrenume(attributes[2]);
        p.setDataNasterii(attributes[3]);
        p.setProfesie(attributes[4]);


         listOfPersons.add(p);
     }
  }
于 2012-05-02T15:42:06.823 回答