Android reading from .txt File created in src folder

前端 未结 2 702
眼角桃花
眼角桃花 2021-01-29 09:24

I am using this to get my File located in src folder src/file1.txt

InputStream is = openFileInput(\"file1.txt\");

and using BufferedReader to R

相关标签:
2条回答
  • 2021-01-29 10:06

    Your src folder doesn't and won't exist on your emulator or device; it only exists on your host machine. If you want to read in a file delivered with your app, put it in either your raw or assets folder.

    0 讨论(0)
  • 2021-01-29 10:19

    can try this code...................................

    String 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();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题