如何在android中将文件写入assets文件夹或raw文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3760626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:01:19  来源:igfitidea点击:

How to write files to assets folder or raw folder in android?

android

提问by dwarkesh

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

我正在开发一些应用程序,我必须从某个 http 位置更新资产/原始文件夹运行时中存在的一些文件。

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

任何人都可以通过分享如何在资产或原始文件夹中写入文件来帮助我吗?

回答by Aaron Saunders

It cannot be done. It is impossible.

这是不可能的。是不可能的。

回答by jing

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

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)

您可能想要研究的其他替代方案是共享偏好和使用缓存文件(所有这些都在上面的链接中描述)

回答by Suryavel TR

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

您不能将数据写入资产/原始文件夹,因为它是打包的(.apk)并且大小不可扩展。

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).

如果您的应用程序需要从服务器下载依赖文件,您可以使用 android 提供的 APK 扩展文件 ( http://developer.android.com/guide/market/expansion-files.html)。

回答by Vikrant

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();
    }
}

回答by Ahsan Idrees

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.

您无法在资产中编写 JSON 文件。如前所述,资产是只读的。但是您可以将资产(json 文件/资产中的任何其他内容)复制到移动设备的本地存储,然后从本地存储进行编辑(写入/读取)。可以使用更多存储选项,例如共享首选项(适用于小数据)和 sqlite 数据库(适用于大数据)。