Android从src文件夹中创建的.txt文件读取

Android从src文件夹中创建的.txt文件读取,android,Android,我用它来获取位于src文件夹src/file1.txt中的文件 InputStream is = openFileInput("file1.txt"); 使用BufferedReader进行读取,但其抛出 FileNotFoundException:data/data/package/files/file1.txt InputStream is = openFileInput("file1.txt"); 同样的事情也发生在 URL fileURL = getClass().getClassL

我用它来获取位于src文件夹src/file1.txt中的文件

InputStream is = openFileInput("file1.txt");
使用BufferedReader进行读取,但其抛出

FileNotFoundException:data/data/package/files/file1.txt

InputStream is = openFileInput("file1.txt");
同样的事情也发生在

URL fileURL = getClass().getClassLoader().getResource("file1.txt");

有什么想法吗?/

你的src文件夹在你的模拟器或设备上不存在,也不会存在;它只存在于您的主机上。如果要读取应用程序附带的文件,请将其放入原始文件夹或资产文件夹。

可以尝试此代码

字符串packageName=context.getPackageName()

DB_PATH=“/data/data/”+packageName+“/databases/”

path=DB_path+DB_path

   private void copyDatabase() {
    try {
        InputStream dbInputStream = context.getAssets().open(DB_NAME);//Read data..........
        String path = DB_PATH + DB_NAME;
        OutputStream dbOutputStream = new FileOutputStream(path);// write data............
        byte[] buffer = new byte[4096];
        int readCount = 0;
        while ((readCount = dbInputStream.read(buffer)) > 0) {
            dbOutputStream.write(buffer, 0, readCount);
        }

        dbInputStream.close();
        dbOutputStream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

将其放入原始文件夹。将其移动到Assets文件夹并使用
InputStream is=getResources().getAssets().open(“file1.txt”)这绝不是对所问问题的回答。