Android通过Json实现导入导出

  1 // 导出文件
  2         private void Export() {
  3                 filePath = null;
  4  
  5                 // 判断sd卡是否存在
  6                 boolean hasSDCard = Environment.getExternalStorageState().equals(
  7                                 Environment.MEDIA_MOUNTED);
  8                 if (hasSDCard) {
  9  
 10                         filePath = Environment.getExternalStorageDirectory().toString()
 11                                         + File.separator + "hello.txt";
 12  
 13                         try {
 14                                 file = new File(Environment.getExternalStorageDirectory(),
 15                                                 "hello.txt");
 16                                 if (file.exists()) {
 17                                         file.delete();
 18                                 }
 19                                 FileOutputStream outStream = new FileOutputStream(file);
 20                                 listNote = CreateData.getListData();
 21                                 JSONArray jsonArray = new JSONArray();
 22                                 try {
 23                                         for (int i = 0; i < listNote.size(); i++) {
 24                                                 json = new JSONObject();
 25                                                 Bug.Print("" + listNote.size());
 26                                                 System.out.println("---->" + listNote.size());
 27                                                 json.put("Id", listNote.get(i).getId());
 28                                                 json.put("Colorid", listNote.get(i).getColorid());
 29                                                 json.put("Context", listNote.get(i).getContext());
 30                                                 json.put("Time", listNote.get(i).getTime());
 31                                                 Bug.Print(json.getString("Colorid"));
 32                                                 jsonArray.put(json);
 33                                         }
 34                                         String s = jsonArray.toString();
 35                                         System.out.println(s);
 36                                         outStream.write(s.getBytes());
 37                                 } catch (JSONException e) {
 38                                         e.printStackTrace();
 39  
 40                                 }
 41                                 Toast.makeText(this.owner, "", Toast.LENGTH_LONG).show();
 42                                 outStream.close();
 43                                 file.createNewFile();
 44  
 45                         } catch (Exception e) {
 46                                 e.printStackTrace();
 47                                 Bug.Print(e.getMessage());
 48                         }
 49                         // 提示框
 50                         AlertDialog.Builder builder = new Builder(this.owner);
 51                         builder.setMessage("导出成功");
 52  
 53                         builder.setTitle("已将文本文件(hello.txt)输出至SD卡" + file + "目录下");
 54  
 55                         builder.setPositiveButton("确认", new OnClickListener() {
 56                                 @Override
 57                                 public void onClick(DialogInterface dialog, int which) {
 58                                         dialog.dismiss();
 59                                 }
 60                         });
 61                         builder.create().show();
 62                 }
 63         }
 64  
 65  
 66  
 67  
 68         // 导入文件
 69         private void Introduction() {
 70                 /*
 71                   
 72                  */
 73                 try {
 74  
 75                         File urlFile = new File(Environment.getExternalStorageDirectory()
 76                                         + "/hello.txt");
 77                         InputStreamReader isr = new InputStreamReader(new FileInputStream(
 78                                         urlFile), "UTF-8");
 79                         BufferedReader br = new BufferedReader(isr);
 80                         str = "";
 81                         String mimeTypeLine = null;
 82  
 83                         while ((mimeTypeLine = br.readLine()) != null) {
 84                                 str = str + mimeTypeLine;
 85  
 86                         }
 87                         System.out.println(str);
 88                         JSONObject obj = null;
 89                         JSONArray jsonArray = null;
 90  
 91                         jsonArray = new JSONArray(str);
 92                         for (int i = 0; i < jsonArray.length(); i++) {
 93                                 obj = jsonArray.getJSONObject(i);
 94  
 95                                 strID = obj.getString("Context");
 96                                 time = obj.getString("Time");
 97                                 colorid = obj.getString("Colorid");
 98  
 99                                 System.out.println(strID);
100                                 CreateData.InsetData(new Entity(strID, time, colorid));
101                         }
102  
103                         Log.d("TAG", str);
104                 } catch (Exception e) {
105  
106                         Toast.makeText(this.owner, "导入失败", Toast.LENGTH_LONG).show();
107                 }
108         }

 

posted @ 2015-04-13 14:53  小菜希  阅读(819)  评论(0编辑  收藏  举报