当前位置:   article > 正文

【Android实战】json文件读取并将数据写入文件_android 写入json数据到一个自定义的config文件

android 写入json数据到一个自定义的config文件
  1. package com.json.ss;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.text.DecimalFormat;
  9. import net.sf.json.JSONArray;
  10. import net.sf.json.JSONObject;
  11. public class JsonReader {
  12. public static void main(String[] args) {
  13. /*String magnetic_x = "";
  14. String magnetic_y = "";
  15. String magnetic_z = "";*/
  16. String magnetic_xyz = "";
  17. DecimalFormat df = new DecimalFormat("0.000");
  18. String sets = ReadFile("d:/a.json");// 获得json文件的内容
  19. JSONObject jo = JSONObject.fromObject(sets);// 格式化成json对象
  20. System.out.println("------------\n" + jo);
  21. System.out.println("\n");
  22. JSONArray jary = jo.getJSONArray("datas");
  23. System.out.println("------------\n" + jary);
  24. for (int i = 0; i < jary.size(); i++) {
  25. JSONObject obj = jary.getJSONObject(i);
  26. // String magnatic=obj.getString("magnetic");
  27. JSONArray magnetic = obj.getJSONArray("magnetic");
  28. System.out.println("------------\n" + magnetic);
  29. System.out.println("\n");
  30. double z_total = 0.0;
  31. double y_total = 0.0;
  32. double x_total = 0.0;
  33. for (int j = 0; j < magnetic.size(); j++) {
  34. JSONObject obj1 = magnetic.getJSONObject(j);
  35. double z = obj1.getDouble("z");
  36. double y = obj1.getDouble("y");
  37. double x = obj1.getDouble("x");
  38. z_total += z;
  39. y_total += y;
  40. x_total += x;
  41. }
  42. double average_x = x_total / magnetic.size();
  43. // magnetic_x = magnetic_x + df.format(average_x) +"\n";
  44. double average_y = y_total / magnetic.size();
  45. // magnetic_y = magnetic_y + df.format(average_y) +"\n";
  46. double average_z = z_total / magnetic.size();
  47. // magnetic_z = magnetic_z + df.format(average_z) +"\n";
  48. double average_xyz = Math.sqrt(average_x * average_x+average_y * average_y+average_z * average_z);
  49. magnetic_xyz = magnetic_xyz + df.format(average_x) + " " + df.format(average_y) + " "
  50. + df.format(average_z) +" "+"-"+df.format(average_xyz)+ "\n";
  51. System.out.println("第" + (i + 1) + "个点的" + "x平均值为:" + average_x
  52. + " " + "y平均值为:" + average_y + " " + "z平均值为:" + average_z
  53. + "\n");
  54. }
  55. /*
  56. * System.out.println("---x---\n"+magnetic_x);
  57. * System.out.println("---y---\n"+magnetic_y);
  58. * System.out.println("---z---\n"+magnetic_z);
  59. */
  60. System.out.println(magnetic_xyz);
  61. String fileName = "long";
  62. try {
  63. writeToFile(fileName,magnetic_xyz);
  64. } catch (IOException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. }
  68. }
  69. private static void writeToFile(String fileName, String result)
  70. throws IOException {
  71. String filePath = "D:\\" + fileName+".txt";
  72. File file = new File(filePath);
  73. if (!file.isFile()) {
  74. file.createNewFile();
  75. DataOutputStream out = new DataOutputStream(new FileOutputStream(
  76. file));
  77. out.writeBytes(result);
  78. }
  79. }
  80. // 读文件,返回字符串
  81. public static String ReadFile(String path) {
  82. File file = new File(path);
  83. BufferedReader reader = null;
  84. String laststr = "";
  85. try {
  86. // System.out.println("以行为单位读取文件内容,一次读一整行:");
  87. reader = new BufferedReader(new FileReader(file));
  88. String tempString = null;
  89. int line = 1;
  90. // 一次读入一行,直到读入null为文件结束
  91. while ((tempString = reader.readLine()) != null) {
  92. // 显示行号
  93. System.out.println("line " + line + ": " + tempString);
  94. laststr = laststr + tempString;
  95. ++line;
  96. }
  97. reader.close();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. } finally {
  101. if (reader != null) {
  102. try {
  103. reader.close();
  104. } catch (IOException e1) {
  105. }
  106. }
  107. }
  108. return laststr;
  109. }
  110. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/319283
推荐阅读
相关标签
  

闽ICP备14008679号