简体   繁体   English

Java-向.txt文件读写ArrayList-BufferedWriter

[英]Java - Writing and Reading ArrayList to and from .txt file - BufferedWriter

Some Background: 一些背景:

I'm making an RPG text based game for class, and I'm using an ArrayList for the inventory, since of course, it will constantly be changing whenever another item is added. 我正在为类制作基于RPG文本的游戏,并且我为清单使用了ArrayList ,因为当然,只要添加了其他项目,它就会不断变化。 I want to be able to save this ArrayList into a txt file and I want to be able to load it back into the same ArrayList so the player can pick up where they left off. 我希望能够将此ArrayList保存到txt文件中,并且希望将其重新加载到相同的ArrayList以便播放器可以从中断处继续播放。

I'm using FileReader and FileWriter , and BufferedReader and BufferedWriter currently. 我目前正在使用FileReaderFileWriter ,以及BufferedReaderBufferedWriter This is because someone else in the class made the save method for me, so I don't really know how to change it to something else if I wanted. 这是因为该类中的其他人为我创建了save方法,所以我真的不知道如何根据需要将其更改为其他方法。 Can I do what I'm trying to do with BufferedWriter ? 我可以使用BufferedWriter做什么吗? I don't really know what kind of code I should put on here, but this is how I declared the ArrayList if that helps: 我真的不知道应该在这里输入哪种代码,但是如果有帮助,这就是我声明ArrayList的方式:

ArrayList<String> bagWeapons = new ArrayList<String>();

And my save method is: 我的保存方法是:

void save(){
    try {
        FileWriter fileWriter =
            new FileWriter(fileName);`

        BufferedWriter bufferedWriter =
            new BufferedWriter(fileWriter);

        //EDIT ZONE         


        bufferedWriter.write(name); //playerName
        bufferedWriter.newLine();
        String hpSave = String.valueOf(hp1); //tempHP conversion
        bufferedWriter.write(hpSave); //playertempHP
        bufferedWriter.newLine();
        String fullhpSave = String.valueOf(fullHP);
        bufferedWriter.write(fullhpSave); //playerfullHP
        bufferedWriter.newLine();


        for(int i=0; i<bagWeapons.size(); i++)
            bufferedWriter.write(bagWeapons.get(i) + ","); //player weapon inventory
        //END EDIT ZONE

        bufferedWriter.close();
    }
    catch(IOException ex) {
        System.out.println(
            "Error writing to file '"
            + fileName + "'");  
    }
}

Does the "for" loop work like that? “ for”循环是否可以那样工作? The print on the text file is: WeaponName,WeaponName,WeaponName, .... 文本文件上的打印内容为:WeaponName,WeaponName,WeaponName等。

I have no idea what I would do to write the ArrayList back. 我不知道该怎么写回ArrayList。 I've tried stuff but my program blows up every time. 我已经尝试过一些东西,但是我的程序每次都会崩溃。

The for loop work like this.To read the data back, you could read the whole string example: weapon1,item2,item99 into a String object and then split the String with a delimiter "," using split function of string class and then build the new array.example. for循环的工作原理是这样的。要读回数据,您可以阅读整个字符串示例:armer1,item2,item99放入String对象,然后使用字符串类的split函数用定界符“,”分割String,然后构建新的array.example。

String text="weapon1,item2,item99";
String parts[]=text.split(",");
for(int i=0;i<parts.length();i++)
  Inventory.add.parts[i]  // or however you set it up.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM