链接列表没有正确读取文件

时间:2012-03-07 01:03:57

标签: java

所以基本上我有一个Linked List类,它包含读取我拥有的文件所需的所有构造函数数据,然后将其转换为链接列表。但是,当它读入文件数据时,它会错误地添加行和空格。我正在使用system.out.println进行检查,它出错了。我不认为这是toString方法,因为我已经搞砸了这么多,没有任何改变。我需要帮助,因为我无法弄明白。 谢谢!

文件信息(基本上所有数据都在不同的行上):

tobi
tobi
tobi@hotmail.com
tobi
Mixed Breed
Male
3-4
Virginia
Walking
lily
lily
lily@hotmail.com
lily
Yorkshire Terrier
Female
3-4
Hawaii
Jumping
peppy
peppy
peppy@hotmail.com
peppy
Chihuahua
Male
7-8
Alaska
Sleeping
fluffy
fluffy
fluffy@hotmail.com
fluffy
MixedBreed
Female
3-4
Virginia
Walking 
flower
flower
flower@hotmail.com
flower
Chihuahua
Female
7-8
Alaska
Sleeping

关联列表:

    import java.io.Serializable;
    import java.util.*;

    public class LinkedAccountList implements Serializable{

    private String username;
    private String password;
    private String email;
    private String name;
    private String breed;
    private String gender;
    private String age;
    private String state;
    private String hobby;

    public LinkedAccountList (String username, String password, String email, String name, String breed, String gender, String age, String state, String hobby){
    this.username = username;
    this.password = password;
    this.email = email;
    this.name = name;
    this.breed = breed;
    this.gender = gender;
    this.age = age;
    this.state = state;
    this.hobby = hobby;
    }

    public String getUsername(){
    return username;
    }

    public String getPassword(){
    return password;
    }

    public String getEmail(){
    return email;
    }

    public String getName(){
    return name;
    }

    public String getBreed(){
    return breed;
    }

    public String getGender(){
    return gender;
    }

    public String getAge(){
    return age;
    }

    public String getState(){
    return state;
    }

    public String getHobby(){
    return hobby;
    }

    @Override
    public String toString() {
    return "Username: "+username+"\nPassword: "+password+"\nEmail: "+email+"\nName: "+name+"\nBreed: "+breed+"\nGender: "+gender+"\nAge: "+age+"\nState: "+state+"\nHobby: "+hobby;
    }

    public void setUsername(String u){
    username = u;
    }

    public void setPassword(String p){
    password = p;
    }

    public void setEmail(String e){
    email = e;
    }

    public void setName(String n){
    name = n;
    }

    public void setBreed(String b){
    breed = b;
    }

    public void setGender(String g){
    gender = g;
    }

    public void setAge(String a){
    age = a;
    }

    public void setState(String s){
    state = s;
    }

    public void setHobby(String h){
    hobby = h;
    }

    }

从文件中读取并创建链接列表方法:

    LinkedList<LinkedAccountList> account = new LinkedList<LinkedAccountList>();

    try
    {
    read(account, "file.txt");
    } catch (Exception e)
    {
    System.err.println(e.toString());
    }
    display(account);
    }

    public static void read(LinkedList<LinkedAccountList> account, String inputFileName) throws java.io.IOException
    {
    BufferedReader infile = new BufferedReader(new FileReader(inputFileName));
    while(infile.ready())
    {
    String username = infile.readLine();
    String password = infile.readLine();
    String email = infile.readLine();
    String name = infile.readLine();
    String breed = infile.readLine();
    String gender = infile.readLine();
    String age = infile.readLine();
    String state = infile.readLine();
    String hobby = infile.readLine();

    LinkedAccountList d = new LinkedAccountList(username, password, email, name, breed, gender, age, state, hobby);
    account.add(d);
    }
    infile.close();
    }

以下是输出的样子(仔细观察,你可以看到问题):

Username: tobi
Password: tobi
Email: tobi@hotmail.com
Name: tobi
Breed: Mixed Breed
Gender: Male
Age: 3-4
State: Virginia
Hobby: Walking
Username: 
Password: lily
Email: lily
Name: lily@hotmail.com
Breed: lily
Gender: Yorkshire Terrier
Age: Female
State: 3-4
Hobby: Hawaii
Username: Jumping
Password:  peppy
Email: peppy
Name: peppy@hotmail.com
Breed: peppy
Gender: Chihuahua
Age: Male
State: 7-8
Hobby: Alaska
Username: Sleeping
Password: fluffy
Email: fluffy
Name: fluffy@hotmail.com
Breed: fluffy
Gender: Mixed Breed
Age: Female
State: 3-4
Hobby: Virginia
Username: Walking 
Password: flower
Email: flower
Name: flower@hotmail.com
Breed: flower
Gender: Chihuahua
Age: Female
State: 7-8
Hobby: Alaska
Username: Sleeping
Password: null
Email: null
Name: null
Breed: null
Gender: null
Age: null
State: null
Hobby: null

3 个答案:

答案 0 :(得分:0)

适合我。问题似乎是您的file.txtWalkinglily之间有一个额外的换行符。仔细检查文件,确保没有文件。

答案 1 :(得分:0)

问题显然不在输出端。 toString()中的错误不会导致它产生类似的输出。

看起来每条记录后输入中都有一个额外的空行。这导致你的read方法在循环的每次迭代中超出一个额外的行。如果这是问题,则修复是添加额外的readLine调用以跳过空白行。


另外,您选择的班级名称特别差。 LinkedAccountList类不代表任何列表。它代表一个帐户。

答案 2 :(得分:0)

您没有任何验证,任何日志记录。 你至少应该创建方法:

public String readLine(BufferedReader br){
    String rl = br.readLine();
    if (rl.trim().length() > 2) {
        return rl;
    }else return readLine(br);
}

并使用它:

...
     while(infile.ready())
        {
          String username = readLine(infile); // instead of String username = infile.readLine();

使用如此长的构造函数参数也容易出错

  

LinkedAccountList(String username,String password,String email,   String name,String breed,String gender,String age,String state,   字符串爱好){

使用ony setX(String arg)方法。这是DAO模式中的常见做法。