爪哇 - 我怎么能写我的ArrayList到一个文件中,并读取(负载)该文件到原来的ArrayList? [英] Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?

查看:293
本文介绍了爪哇 - 我怎么能写我的ArrayList到一个文件中,并读取(负载)该文件到原来的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java编写一个程序,它显示了一系列课外俱乐部(如足球,曲棍球 - 由用户输入)的。俱乐部加入到下面的的ArrayList

私人的ArrayList<俱乐部GT;俱乐部=新的ArrayList<俱乐部GT;();

由followng方法:

公共无效addClub(字符串clubName){
    俱乐部会所​​= findClub(clubName);
    如果(俱乐部== NULL)
        clubs.add(新俱乐部(clubName));
}

俱乐部是一个构造函数的类 - 名称:

公共类俱乐部{    私人字符串名称;    市民俱乐部(字符串名称){
        this.name =名称;
    }    //有在我的计划更多的方法,但不影响我的查询..
}

我的计划是工作 - 它让我添加一个新的俱乐部对象到我的ArrayList中,我可以看到ArrayList中,我可以删除任何我想要等。

不过,我现在要保存的ArrayList(俱乐部)到一个文件,然后我希望以后能够加载文件和相同的ArrayList是有一次。

我有这两种方法(见下文),并一直试图得到它的工作,但还没有过anyluck,任何帮助或建议将AP preciated。

Save方法(文件名是由用户选择)

公共无效保存(字符串文件名)抛出FileNotFoundException异常{
    串TMP = clubs.toString();
    PrintWriter的PW =的新PrintWriter(新的FileOutputStream(文件名));
    pw.write(TMP);
    pw.close();
}

Load方法(当前code不会运行 - 文件是一个字符串,但需要俱乐部

公共无效负载(字符串文件名)抛出FileNotFoundException异常{
    的FileInputStream FILEIN =新的FileInputStream(文件名);
    扫描程序扫描=新的扫描仪(FILEIN);
    串loadedClubs = scan.next();
    clubs.add(loadedClubs);
}

我也使用GUI运行应用程序,并在那一刻,我可以点击我的保存按钮,然后让我键入一个名称和位置,并将其保存。该文件显示,并且可以在记事本中,但显示(在我的列表中为每个俱乐部)被辟为像俱乐部@ c5d8jdj


解决方案

作为练习,我建议做以下内容:

 公共无效保存(字符串文件名)抛出FileNotFoundException异常{
    PrintWriter的PW =的新PrintWriter(新的FileOutputStream(文件名));
    对于(俱乐部俱乐部:俱乐部)
        pw.println(club.getName());
    pw.close();
}

这会写每家具乐部的名字在你的文件中的新行。


 足球

足球
排球
...


我将离开加载到你。 提示:你只能一次写一行,就可以读取一行在一个时间

Java所有的类继承了对象类。因此,你可以重写它的方法。在这种情况下,你应该有兴趣由的toString()方法。在你的俱乐部类,则可以覆盖它来打印有关的任何格式的类,你想要一些消息。

 公共字符串的toString(){
    回到俱乐部+姓名;
}

您可以再更改上面的code为:

 公共无效保存(字符串文件名)抛出FileNotFoundException异常{
    PrintWriter的PW =的新PrintWriter(新的FileOutputStream(文件名));
    对于(俱乐部俱乐部:俱乐部)
         pw.println(俱乐部); //对俱乐部调用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();
}

这篇关于爪哇 - 我怎么能写我的ArrayList到一个文件中,并读取(负载)该文件到原来的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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