How to write files to assets folder or raw folder

2019-01-01 11:49发布

问题:

I am working on some application where I have to update some files present in assets / raw folder runtime from some http location.

Can anyone help me to by sharing how to write files in assets or raw folder?

回答1:

It cannot be done. It is impossible.



回答2:

Why not update the files on the local file system instead? You can read/write files into your applications sandboxed area.

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Other alternatives you may want to look into are Shared Perferences and using Cache Files (all described at the link above)



回答3:

You cannot write data\'s to asset/Raw folder, since it is packed(.apk) and not expandable in size.

If your application need to download dependency files from server, you can go for APK Expansion Files provided by android (http://developer.android.com/guide/market/expansion-files.html).



回答4:

Another approach for same issue may help you Read and write file in private context of application

                 String NOTE = \"note.txt\";  
                 private void writeToFile() {
        try {
         OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
                NOTES, 0));

         out.write(\"testing\");
         out.close();
         }

        catch (Throwable t) {
        Toast.makeText(this, \"Exception: \" + t.toString(), 2000).show();
        }
             }


           private void ReadFromFile()
      {
        try {
        InputStream in = openFileInput(NOTES);
        if (in != null) {
            InputStreamReader tmp = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(tmp);
            String str;
            StringBuffer buf = new StringBuffer();
            while ((str = reader.readLine()) != null) {
                buf.append(str + \"\\n\");
            }
            in.close();
            String temp = \"Not Working\";
            temp = buf.toString();
            Toast.makeText(this, temp, Toast.LENGTH_SHORT).show();
        }
    } catch (java.io.FileNotFoundException e) {
        // that\'s OK, we probably haven\'t created it yet
    } catch (Throwable t) {
        Toast.makeText(this, \"Exception: \" + t.toString(), 2000).show();
    }
}


回答5:

You Can\'t write JSON file while in assets. as already described assets are read-only. But you can copy assets (json file/anything else in assets ) to local storage of mobile and then edit(write/read) from local storage. More storage options like shared Preference(for small data) and sqlite database(for large data) are available.



标签: android