如何JSON数据写入到Android的文件文件、数据、JSON、Android

2023-09-07 10:01:51 作者:瘋了ф也是

有人能解释这是怎么回事错在这里我使用下面的函数

Can someone explain what is going wrong here I am using following function

public void WriteSettings(Context context, String data){ 
    FileOutputStream fOut = null; 
    OutputStreamWriter osw = null;

    try{
     fOut = context.openFileOutput("schemas.json",Context.MODE_APPEND);       
     osw = new OutputStreamWriter(fOut); 
     osw.write(data); 
     osw.flush(); 
     Toast.makeText(context, data+"Data",Toast.LENGTH_SHORT).show();
     Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
     } 
     catch (Exception e) {       
     e.printStackTrace(); 
     Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
     } 
     finally { 
        try { 
               osw.close(); 
               fOut.close(); 
               } catch (IOException e) { 
               e.printStackTrace(); 
               } 
     } 
}

和与我的一次HTTP请求完成后调用

And calling with once my http request is completed

的JSONObject json_res = currentDFD.getJSONObject(结果);WriteSettings(getBaseContext(),json_res.toString());

结果示于举杯提醒但是不写入文件该文件位于资产文件夹在此先感谢

The result is alerted in toast however not written to the file the file is located in assets folder thanks in advance

推荐答案

据我所知你不能。资产文件夹是只读的在运行时。

AFAIK You can't. The assets folder is read-only at runtime.

选择一个不同的位置来保存数据,请参阅数据存储在Android中了解详情。

Pick a different location to save your data, see Data Storage in Android for more information.

资产文件夹是文件夹类似资源,SRC,根等,这些都是有用的,提供不同的文件作为输入来构建系统生成APK文件,为您的应用程序。

The assets folder is like folders res, src, gen, etc. These are all useful to provide different files as input to build system to generate APK file for your app.

所有这些都是只读的,而你的应用程序正在运行。在运行时,你只能写SD卡。

All these are read-only while your app is running. At run-time you can only write to SD card.