将链接列表保存到文件(元素不正确)

时间:2012-03-13 22:28:58

标签: java io linked-list

我需要将我创建的链表保存到文件中,但我希望用户帐户的每个部分都是自己的元素。即(用户名,密码,电子邮件,姓名,品种,性别,年龄,州,爱好)。但是我的代码出了问题,每个帐户都是自己的元素。任何帮助都会很棒!

此处还有指向我的帐户类的链接,用于创建链接列表 http://pastebin.com/jnBrcnP1

链接列表如下所示:

tobi
tobi123
tobi@hotmail.com
tobi
Mixed Breed
Male
1-2
Virginia
Walking
peppy
peppy123
peppy@hotmail.com
peppy
Chihuahua
Male
5-6
Virginia
Eating

保存到这样的文件:

tobitobi123tobi@hotmail.comtobiMixed BreedMale1-2VirginiaWalking
peppypeppy123peppy@hotmail.compeppyChihuahuaMale5-6VirginiaEating

创建链接列表的代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
public class Main extends javax.swing.JFrame implements ActionListener{

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

public static void main(String[] args) {

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

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

    public static void read(LinkedList<Account> account, String inputFileName) throws java.io.IOException
    {
        BufferedReader infile = new BufferedReader(new FileReader(inputFileName));
        while(infile.ready())
        {

            String username = readLine(infile);
            String password = readLine(infile);
            String email = readLine(infile);
            String name = readLine(infile);
            String breed = readLine(infile);
            String gender = readLine(infile);
            String age = readLine(infile);
            String state = readLine(infile);
            String hobby = readLine(infile);

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

    public static void display(LinkedList<?> c)
    {
        for (Object e : c)
        {
            System.out.println(e);
        }
    }

将链接列表保存到文件的代码:

        String file_name = "output.txt";
        try {

                FileWriter fstream = new FileWriter(file_name);
                BufferedWriter out = new BufferedWriter(fstream);

                ListIterator itr = account.listIterator();
                while (itr.hasNext()) {
                    Account element = (Account) itr.next();
                    out.write("" + element);
                    out.newLine();
                }

                out.close();
                System.out.println("File created successfully.");

        } catch (Exception e) {
        }

1 个答案:

答案 0 :(得分:1)

这是Account中的问题:

public String toString() {
    return ""+username+"\n"+password+"\n"+email+"\n"+name+
           "\n"+breed+"\n"+gender+"\n"+age+"\n"+state+"\n"+hobby;
}

假设 \n是合适的行结尾。我的猜测是你在Windows上运行,它将是\r\n。就个人而言,我认为你的“编写”代码最好不要使用toString(),而是写出行本身 - 毕竟,它知道它想要使用的格式。

(另外,我会advise against使用"" + ...作为将值转换为字符串的方式...)

相关问题