简体   繁体   English

如何在 Android App 中将 ArrayList 写入文本文件

[英]How to write an ArrayList into a text file in Android App

So I have an ArrayList of some data which is needed for the app, and I don't have any problem to load the data from a text file named MyData.txt and store it in an ArrayList.因此,我有一个 ArrayList,其中包含应用程序所需的一些数据,并且从名为 MyData.txt 的文本文件加载数据并将其存储在 ArrayList 中没有任何问题。 But the problem is, after some changes in the data which is stored in loaded ArrayList by the user I want the data that stored into the MyData.txt get updated with the new ArrayList.但问题是,在用户对存储在加载的 ArrayList 中的数据进行一些更改后,我希望使用新的 ArrayList 更新存储到 MyData.txt 中的数据。 I write some codes for that but it doesn't seem to work and my txt file is not getting updated after I press the save button, it just jumped out of the app.我为此编写了一些代码,但它似乎不起作用,并且在我按下保存按钮后我的 txt 文件没有得到更新,它只是跳出了应用程序。

 public void SaveData(List<Four> list) throws IOException {
    FileOutputStream file = openFileOutput("MyData.txt", MODE_PRIVATE);
    OutputStreamWriter outputFile = new OutputStreamWriter(file);
    for(int i=0 ; i<list.size() ; i++){
        outputFile.write(list.get(i).first+";"+list.get(i).second+";"+list.get(i).second+";"+list.get(i).fourth+"\n");
    }
    outputFile.flush();
    outputFile.close();
}

I have an ArrayList with the data type of Four ( Four is a class which includes "String(showed as first), String(showed as second, int (showed as third), int (showed as fourth)) the output of the code should be stored in a text file, for example, if My list just had one index like "John Brown 1938494 0" my text file should look like "John;Brown;1938494;0".我有一个数据类型为四的 ArrayList(四是 class,其中包括“字符串(显示为第一个)、字符串(显示为第二个、int(显示为第三个)、int(显示为第四个))代码的 Z78E6221F6393D1396668应该存储在一个文本文件中,例如,如果我的列表只有一个索引,如“John Brown 1938494 0”,我的文本文件应该看起来像“John;Brown;1938494;0”。

if you need to know anything else about the code just tell me.如果您需要了解有关代码的任何其他信息,请告诉我。 thanks for your time.谢谢你的时间。

You can use google gson.您可以使用谷歌 gson。

    class Four {
    String first,second;
    int third,fourth;
    public Four()
    {

    }
}
 //save_array(your_context,"MyData.txt",your_array)
void save_array(Context contxt,String file_name,ArrayList<Four> testes)
{
    Gson gson = new Gson();
    String json = gson.toJson(testes);
    String root = contxt.getExternalFilesDir(null).getAbsolutePath() + "/arrays";


    File file = new File(root);
    file.mkdirs();
    try {
        File out_file = new File(file, file_name);
        if(out_file.exists()){
            out_file.delete();
        }
        FileWriter writer = new FileWriter(out_file,true);
        writer.append(json);
        writer.flush();
        writer.close();
    }catch (Exception ex)
    {

    }
}
ArrayList<Four> saved_array(Context contxt, String file_name) throws IOException {
    String path = contxt.getExternalFilesDir(null).getAbsolutePath() + "/arrays/"+file_name;


    Gson gson = new Gson();
    BufferedReader br = new BufferedReader(new FileReader(path));
    return gson.fromJson(br, new TypeToken<List<Four>>(){}.getType());


} 

you can call as Remember to request for storage permissions你可以调用记住来请求存储权限

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

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