Android通过Json实现导入导出
2015-04-13 14:53
190 查看
// 导出文件 private void Export() { filePath = null; // 判断sd卡是否存在 boolean hasSDCard = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); if (hasSDCard) { filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "hello.txt"; try { file = new File(Environment.getExternalStorageDirectory(), "hello.txt"); if (file.exists()) { file.delete(); } FileOutputStream outStream = new FileOutputStream(file); listNote = CreateData.getListData(); JSONArray jsonArray = new JSONArray(); try { for (int i = 0; i < listNote.size(); i++) { json = new JSONObject(); Bug.Print("" + listNote.size()); System.out.println("---->" + listNote.size()); json.put("Id", listNote.get(i).getId()); json.put("Colorid", listNote.get(i).getColorid()); json.put("Context", listNote.get(i).getContext()); json.put("Time", listNote.get(i).getTime()); Bug.Print(json.getString("Colorid")); jsonArray.put(json); } String s = jsonArray.toString(); System.out.println(s); outStream.write(s.getBytes()); } catch (JSONException e) { e.printStackTrace(); } Toast.makeText(this.owner, "", Toast.LENGTH_LONG).show(); outStream.close(); file.createNewFile(); } catch (Exception e) { e.printStackTrace(); Bug.Print(e.getMessage()); } // 提示框 AlertDialog.Builder builder = new Builder(this.owner); builder.setMessage("导出成功"); builder.setTitle("已将文本文件(hello.txt)输出至SD卡" + file + "目录下"); builder.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } } // 导入文件 private void Introduction() { /* */ try { File urlFile = new File(Environment.getExternalStorageDirectory() + "/hello.txt"); InputStreamReader isr = new InputStreamReader(new FileInputStream( urlFile), "UTF-8"); BufferedReader br = new BufferedReader(isr); str = ""; String mimeTypeLine = null; while ((mimeTypeLine = br.readLine()) != null) { str = str + mimeTypeLine; } System.out.println(str); JSONObject obj = null; JSONArray jsonArray = null; jsonArray = new JSONArray(str); for (int i = 0; i < jsonArray.length(); i++) { obj = jsonArray.getJSONObject(i); strID = obj.getString("Context"); time = obj.getString("Time"); colorid = obj.getString("Colorid"); System.out.println(strID); CreateData.InsetData(new Entity(strID, time, colorid)); } Log.d("TAG", str); } catch (Exception e) { Toast.makeText(this.owner, "导入失败", Toast.LENGTH_LONG).show(); } }
相关文章推荐
- Android手机客户端通过JSP实现与Tomcat服务器端通信(Msql数据库,Json作为载体)--客户端代码
- Android中通过annotation实现java对象和json的转换
- java通过jxls框架实现导入导出excel
- java通过jxl.jar实现excel导入导出
- 通过SQL语句直接实现Excel与数据库的导入导出
- 通过SQL语句直接实现Excel与数据库的导入导出
- 通过tableExport.js插件来实现导出Excel/Pdf/txt/json等
- Android通过类对象的方式实现JSON数据的解析
- 通过SQL语句直接实现Excel与数据库的导入导出
- 在web端和android端通过JSON格式实现数据交互
- 通过SQL语句直接实现Excel与数据库的导入导出
- 通过SQL语句直接实现Excel与数据库的导入导出
- 通过sqoop 实现hdfs与mysql的数据导入导出
- 通过SQL语句直接实现Excel与数据库的导入导出
- Android中如何实现excel的导入/导出
- java通过jxls框架实现导入导出excel
- Android 实现json网络数据通过BaseAdapter加载到ListView中
- 编写一个通讯录,功能与以前的诺基亚手机一样,可以通过键盘进行箭头的移动,回车进入选项,用“W”键代表手机手机左键,“backspace”键代表返回上一级。能够实现基本的人性化的增删改查及文件的导入导出
- 通过标准输入输出实现Greenplum文件导入导出
- java通过jxl.jar实现excel导入导出