package com.example.savejsonproject;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {
private String company="宜昌静哥科技软件学院";
private String address="湖北省宜昌市宜都市枝城镇";
private String telephone="18671736137";
private String[] namedata={"李元静","冯新尧","何帆"};
private int[] agedata={21,22,21};
private boolean[] marrieddata={true,false,true};
private double[] salarydata={8000.0,8000.0,8000.0};
private Date[] birthdaydata={new Date(),new Date(),new Date()};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
JSONObject allData=new JSONObject(); //建立最外面的JSONObject
JSONArray array=new JSONArray(); //定义新的JSONArray对象
for (int i = 0; i < namedata.length; i++) { //For循环添加数据
JSONObject temp=new JSONObject(); //创建一个新的JSONObject对象
try {
temp.put("name", namedata[i]); //设置要保存的数据,直接子项的子项
temp.put("age", agedata[i]);
temp.put("merried", marrieddata[i]);
temp.put("salary", salarydata[i]);
temp.put("birthday", birthdaydata[i]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
array.put(temp);
}
try {
allData.put("persondata", array); //保存所有数据
allData.put("company", this.company); //最外层数据
allData.put("address", this.address);
allData.put("telephone", this.telephone);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//判断是否存在SD卡
return ;
}
File file=new File(Environment.getExternalStorageDirectory().toString()
+File.separator+"mldndata"
+File.separator+"json.txt");
if(!file.getParentFile().exists()){//判断父文件是否存在,如果不存在则创建
file.getParentFile().mkdirs();
}
PrintStream out=null; //打印流
try {
out=new PrintStream(new FileOutputStream(file)); //实例化打印流对象
out.print(allData.toString()); //输出数据
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){ //如果打印流不为空,则关闭打印流
out.close();
}
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. Android(安卓)Stagefright MPEG4Extractor分析
  4. [置顶] Android加载数据过程中的菊花显示
  5. Android(安卓)Wifi相关广播 -- WIFI_STATE_CHANGED_ACTION
  6. android webview 中网页数据与js交互
  7. Android/Linux大于1024Bytes打印
  8. Android(安卓)解决 RecyclerView 嵌套 ScrollView 数据显示不全
  9. Android(安卓)kernel printk打印级别

随机推荐

  1. PHP:函数的返回值和参数,实例回调函数,匿
  2. 【php基础入门】小白整理PHP常用的字符串
  3. 视频:使用Docker搭建RabbitMQ环境
  4. 推荐使用 SSH 方式连接 Git 服务
  5. 收藏分享:众多PPT模板,分享给需要的你!
  6. 深入理解[代理模式]原理与技术
  7. ECMAScript 2019(ES10)新特性简介
  8. 工作流程引擎的退回规则
  9. 一个SQL让导致整个数据库都整挂了!
  10. 扫雷