Java 使用存储在“资源”文件夹中的文本文件

Java 使用存储在“资源”文件夹中的文本文件,java,android,assets,Java,Android,Assets,我试图理解存储在assets文件夹中的数据文件与存储在res/raw文件夹中的数据文件之间的区别。我的目标是在应用程序目录结构中存储一个数据文件,在这里(本例中)存储测试分数,可以访问它们,然后可以修改它们。我的理解是,我需要为此使用资产而不是原始文件 当文本文件存储在RES/RAW文件夹中时,我设法将文本文件中的数据加载到一个数组中,但现在我使用的是资产文件,因此无法使其工作。我认为这和使用AssetManager.open一样简单 最终我的一个问题是。如何读取和写入资产文件夹中的文本文件 这

我试图理解存储在assets文件夹中的数据文件与存储在res/raw文件夹中的数据文件之间的区别。我的目标是在应用程序目录结构中存储一个数据文件,在这里(本例中)存储测试分数,可以访问它们,然后可以修改它们。我的理解是,我需要为此使用资产而不是原始文件

当文本文件存储在RES/RAW文件夹中时,我设法将文本文件中的数据加载到一个数组中,但现在我使用的是资产文件,因此无法使其工作。我认为这和使用AssetManager.open一样简单

最终我的一个问题是。如何读取和写入资产文件夹中的文本文件

这是我的密码:

public class MainActivity extends AppCompatActivity {

    String[][] testScoreList = new String[3][3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Load test scores into arraylist
        nameArrayListMethod();
    }

   //This method loads test scores into an array
    public void nameArrayListMethod (){
        InputStreamReader InputSR = null;
        BufferedReader BufferedRdr = null;
        String thisLine = null;

        AssetManager am = getAssets();

        InputSR = new InputStreamReader(am.open("test_scores.txt"));
        BufferedRdr = new BufferedReader(InputSR);


        try {
            // open input stream test_scores for reading purpose.
            int i = 0;
            while ((thisLine = BufferedRdr.readLine()) != null) {
                // System.out.println(thisLine);

                String[] parts = thisLine.split(" ");
                testScoreList[i][0] = parts[0];
                testScoreList[i][1] = parts[1];
                i = i +1;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
我在
(am.open(“test\u scores.txt”)
行上得到一个
未处理的异常:java.io.IOException
eror


为输入喝彩

尝试使用Try/catch防止应用程序崩溃。他们在那里是有原因的

InputStream is = getResources().openRawResource(R.raw.yourTxtFile);
String s = IOUtils.toString(is); // Here is the whole string you want
IOUtils.closeQuietly(is); // don't forget to close your streams
这是我在Android中打开文件的方式:

try {
    InputStream inputstream = getResources().openRawResource(getResources().getIndentifier("file_name", "folder", getPackageName()));
    BufferedReader bufferedRdr = new BufferedReader(inputstream);
} catch (IOException ioe) {
    ioe.printStackTrace(); // error handling should be better then this...
} // and you should close the InputStream and BufferedReader with .close().
AssetManger#open(String)将抛出异常,您需要处理它

public final InputStream open(String fileName) throws IOException {
    return open(fileName, ACCESS_STREAMING);
}
因此,您需要:

   try {
        InputSR = new InputStreamReader(am.open("test_scores.txt"));
        BufferedRdr = new BufferedReader(InputSR);
        // open input stream test_scores for reading purpose.
        int i = 0;
        while ((thisLine = BufferedRdr.readLine()) != null) {
            // System.out.println(thisLine);

            String[] parts = thisLine.split(" ");
            testScoreList[i][0] = parts[0];
            testScoreList[i][1] = parts[1];
            i = i +1;
        }
    } catch (Exception e) {
        e.printStackTrace();

    }

如果你使用R.raw引用,比如David建议的,获得IOException的机会非常低,那么使用R.raw是一个很好的方法。可能是程序员优先。如何以及何时处理异常的低概率。我尝试处理它们,这样应用程序就不会在没有任何明显原因的情况下崩溃。但这是另一种讨论;)我没有使用R.raw中的文件。我需要访问的文件位于assets文件夹中。据我所知,我需要使用assest文件夹而不是raw文件夹来允许用户更新和保存数据文件。您可能不需要担心这一点。你可以使用只有你的应用程序才能看到的私人文件。。。为什么会抛出异常?文件test_scores.txt确实存在。顺便说一句,你的解决方案是有效的,但我不明白为什么。它不会引发异常,但可能是由于不可预见的事情。阅读和写作是其中一种风险更大的事情。当与服务器交谈时,情况会变得更糟…除了我的文件在资产文件夹中,而不是资源原始文件夹中。我需要在资源中工作,以便可以在用户设备上保存和修改文件。
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open("myfile.txt")));
        String line;
        while ((line = br.readLine()) != null) {
           System.out.println(line);
        }
    } catch (IOException e) {    
    }