如何JSON数据写入到Android的文件 [英] how to write json data to a file in android

查看:721
本文介绍了如何JSON数据写入到Android的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于如何JSON数据写入到Android的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆