简体   繁体   English

在哪里放置设置文件?

[英]Where to put a settings file?

Okay, so I've got a couple of data files for an android app. 好的,所以我有几个用于Android应用程序的数据文件。 The files contain lists of items and variables for each, so I don't think using the "Key-Value" method would work very well. 这些文件包含每个项的列表和变量,因此我认为使用“键值”方法效果不佳。

I originally had the files in the "raw" folder, but then I learned that you can't write to that location and this file does need to be updatable. 我最初将文件保存在“原始”文件夹中,但是后来我知道您无法写入该位置,因此该文件确实需要更新。 I now have it reading and writing from (afaik) the root directory of my program's internal storage (File file = new File(getApplicationContext().getFilesDir(), "settings.txt"); ), and that works alright. 现在,我可以从(afaik)读取程序内部存储的根目录(文件file = new File(getApplicationContext()。getFilesDir(),“ settings.txt”);),并且可以正常运行。 But what I'd like to be able to do is have a default settings file that is placed when I install/update the app. 但是我想做的是在安装/更新应用程序时放置一个默认设置文件。

For example, when I was using the "raw" folder, I could put a file in the "raw" directory on my computer and have it show up when I run the app. 例如,当我使用“原始”文件夹时,可以将文件放在计算机上的“原始”目录中,并在运行应用程序时显示它。 Can I get this behaviour with a location my app can also write to, and if so, how? 我可以使用我的应用程序也可以写入的位置来获得这种行为吗?

Edit: OK to try and clarify, what I'm looking for is a location that I can a) have my app read and write data to on my phone b) can have a file put in that location when the app is installed/updated, like you can put a file in the raw folder on your PC and have your app able to read it. 编辑:可以尝试澄清一下,我正在寻找的位置是我可以的位置a)让我的应用程序在手机上读写数据b)在安装/更新应用程序时可以在该位置放置文件,就像您可以将文件放在PC上的原始文件夹中一样,并使您的应用能够读取该文件。

I just want to know if this is possible. 我只想知道这是否可行。

As far as I know, you can create a folder within your application's root directory to save your files to. 据我所知,您可以在应用程序的根目录中创建一个文件夹来保存文件。 From there, you should be able to create, read, update, and delete files. 从那里,您应该能够创建,读取,更新和删除文件。

File dir = new File(getApplicationContext().getFilesDir(),"directory name");
if(!dir.exists()) {
    try {
        dir.mkdir();
    } catch (SecurityException e) {
        //deal with exception
    }
}
File settings = new File(dir, "settings.txt");
if(!settings.exists()) {
    try {
        settings.createNewFile();
    } catch (SecurityException e) {
        //deal with exception
    }
}

Maybe this will work for you? 也许这对您有用吗?

android may throw it's own form of SecurityException android可能会抛出它自己的形式的SecurityException


I would suggest you create a Java class that creates, reads, and writes to a settings.txt file. 我建议您创建一个Java类,该类创建,读取和写入settings.txt文件。 If you want to have that class also create a default settings.txt file and make it write that file to the folder of choice if a settings.txt file doesn't exist already. 如果您想拥有该类,还可以创建一个默认的settings.txt文件,并将其写入选择的文件夹(如果settings.txt文件尚不存在)。

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

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