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

标签 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) {
        }

最佳答案

这就是帐户中的问题:

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 使用 ""+ ... 作为将值转换为字符串的方式...)

关于java - 将链接列表保存到文件(元素不正确),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9693172/

相关文章:

java - MALICIOUS_CODE EI_EXPOSE_REP 中等

java - Android http请求POST

linux - 在流程调用之间传输数据

c++ - 在 C++ 中以 'D' 作为指数前缀读取双科学记数法

java - 每个客户端一个线程。可行吗?

java - 用 | 分割字符串java中的分隔符

java - 在 groovy 脚本 (soapui) 中导入 jar 时出错

c - 投票系统实现中的错误

C++ 链表 : Overload bracket operators []

Free'ing C 中的链表结构的混淆