Java - 如何将我的 ArrayList 写入文件,并将该文件读取(加载)到原始 ArrayList? [英] Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?

查看:36
本文介绍了Java - 如何将我的 ArrayList 写入文件,并将该文件读取(加载)到原始 ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 编写一个程序,该程序显示一系列课后俱乐部(例如足球、曲棍球 - 由用户输入).俱乐部被添加到以下ArrayList:

私有ArrayListclubs = new ArrayList();

通过以下方法:

public void addClub(String clubName) {Club club = findClub(clubName);如果(俱乐部 == 空)俱乐部.添加(新俱乐部(俱乐部名称));}

'Club' 是一个带有构造函数的类 - 名称:

public class Club {私人字符串名称;公共俱乐部(字符串名称){this.name = 名称;}//我的程序中有更多的方法但不影响我的查询..}

我的程序正在运行 - 它允许我将一个新的俱乐部对象添加到我的数组列表中,我可以查看数组列表,我可以删除任何我想要的等等.

但是,我现在想将该数组列表(俱乐部)保存到一个文件中,然后我希望能够稍后加载该文件并且再次出现相同的数组列表.

我有两种方法(见下文),并且一直试图让它工作但没有任何运气,任何帮助或建议将不胜感激.

保存方法(文件名由用户选择)

public void save(String fileName) throws FileNotFoundException {String tmp = clubs.toString();PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));pw.write(tmp);pw.close();}

加载方法(当前代码不会运行 - 文件是一个字符串但需要是俱乐部?

public void load(String fileName) throws FileNotFoundException {FileInputStream fileIn = new FileInputStream(fileName);Scanner scan = new Scanner(fileIn);String loadedClubs = scan.next();clubs.add(loadedClubs);}

我也在使用 GUI 来运行应用程序,目前,我可以单击保存"按钮,然后我可以输入名称和位置并保存它.该文件出现并可以在记事本中打开,但显示为类似 Club@c5d8jdj(对于我列表中的每个俱乐部)

解决方案

作为练习,我建议执行以下操作:

public void save(String fileName) 抛出 FileNotFoundException {PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));对于(俱乐部俱乐部:俱乐部)pw.println(clu​​b.getName());pw.close();}

这会将每个俱乐部的名称写在您文件的新行上.

<块引用>

足球棋足球排球...

我会把加载留给你.提示:你一次写一行,然后一次读一行.

Java 中的每个类都扩展了Object 类.因此,您可以覆盖其方法.在这种情况下,您应该对 toString() 方法感兴趣.在您的 Club 类中,您可以覆盖它以按您喜欢的任何格式打印有关该类的一些消息.

public String toString() {返回俱乐部:"+名称;}

然后您可以将上面的代码更改为:

public void save(String fileName) 抛出 FileNotFoundException {PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));对于(俱乐部俱乐部:俱乐部)pw.println(俱乐部);//在 club 上调用 toString(),就像 club.toString()pw.close();}

I am writing a program in Java which displays a range of afterschool clubs (E.G. Football, Hockey - entered by user). The clubs are added into the following ArrayList:

private ArrayList<Club> clubs = new ArrayList<Club>();

By the followng Method:

public void addClub(String clubName) {
    Club club = findClub(clubName);
    if (club == null)
        clubs.add(new Club(clubName));
}

'Club' is a class with a constructor - name:

public class Club {

    private String name;

    public Club(String name) {
        this.name = name;
    }

    //There are more methods in my program but don't affect my query..
}

My program is working - it lets me add a new Club Object into my arraylist, i can view the arraylist, and i can delete any that i want etc.

However, I now want to save that arrayList (clubs) to a file, and then i want to be able to load the file up later and the same arraylist is there again.

I have two methods for this (see below), and have been trying to get it working but havent had anyluck, any help or advice would be appreciated.

Save Method (fileName is chosen by user)

public void save(String fileName) throws FileNotFoundException {
    String tmp = clubs.toString();
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    pw.write(tmp);
    pw.close();
}

Load method (Current code wont run - File is a string but needs to be Club?

public void load(String fileName) throws FileNotFoundException {
    FileInputStream fileIn = new FileInputStream(fileName);
    Scanner scan = new Scanner(fileIn);
    String loadedClubs = scan.next();
    clubs.add(loadedClubs);
}

I am also using a GUI to run the application, and at the moment, i can click my Save button which then allows me to type a name and location and save it. The file appears and can be opened up in Notepad but displays as something like Club@c5d8jdj (for each Club in my list)

解决方案

As an exercise, I would suggest doing the following:

public void save(String fileName) throws FileNotFoundException {
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    for (Club club : clubs)
        pw.println(club.getName());
    pw.close();
}

This will write the name of each club on a new line in your file.

Soccer
Chess
Football
Volleyball
...

I'll leave the loading to you. Hint: You wrote one line at a time, you can then read one line at a time.

Every class in Java extends the Object class. As such you can override its methods. In this case, you should be interested by the toString() method. In your Club class, you can override it to print some message about the class in any format you'd like.

public String toString() {
    return "Club:" + name;
}

You could then change the above code to:

public void save(String fileName) throws FileNotFoundException {
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    for (Club club : clubs)
         pw.println(club); // call toString() on club, like club.toString()
    pw.close();
}

这篇关于Java - 如何将我的 ArrayList 写入文件,并将该文件读取(加载)到原始 ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆