【Android實戰】json檔案讀取並將資料寫入檔案
阿新 • • 發佈:2019-01-03
package com.json.ss; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.text.DecimalFormat; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonReader { public static void main(String[] args) { /*String magnetic_x = ""; String magnetic_y = ""; String magnetic_z = "";*/ String magnetic_xyz = ""; DecimalFormat df = new DecimalFormat("0.000"); String sets = ReadFile("d:/a.json");// 獲得json檔案的內容 JSONObject jo = JSONObject.fromObject(sets);// 格式化成json物件 System.out.println("------------\n" + jo); System.out.println("\n"); JSONArray jary = jo.getJSONArray("datas"); System.out.println("------------\n" + jary); for (int i = 0; i < jary.size(); i++) { JSONObject obj = jary.getJSONObject(i); // String magnatic=obj.getString("magnetic"); JSONArray magnetic = obj.getJSONArray("magnetic"); System.out.println("------------\n" + magnetic); System.out.println("\n"); double z_total = 0.0; double y_total = 0.0; double x_total = 0.0; for (int j = 0; j < magnetic.size(); j++) { JSONObject obj1 = magnetic.getJSONObject(j); double z = obj1.getDouble("z"); double y = obj1.getDouble("y"); double x = obj1.getDouble("x"); z_total += z; y_total += y; x_total += x; } double average_x = x_total / magnetic.size(); // magnetic_x = magnetic_x + df.format(average_x) +"\n"; double average_y = y_total / magnetic.size(); // magnetic_y = magnetic_y + df.format(average_y) +"\n"; double average_z = z_total / magnetic.size(); // magnetic_z = magnetic_z + df.format(average_z) +"\n"; double average_xyz = Math.sqrt(average_x * average_x+average_y * average_y+average_z * average_z); magnetic_xyz = magnetic_xyz + df.format(average_x) + " " + df.format(average_y) + " " + df.format(average_z) +" "+"-"+df.format(average_xyz)+ "\n"; System.out.println("第" + (i + 1) + "個點的" + "x平均值為:" + average_x + " " + "y平均值為:" + average_y + " " + "z平均值為:" + average_z + "\n"); } /* * System.out.println("---x---\n"+magnetic_x); * System.out.println("---y---\n"+magnetic_y); * System.out.println("---z---\n"+magnetic_z); */ System.out.println(magnetic_xyz); String fileName = "long"; try { writeToFile(fileName,magnetic_xyz); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void writeToFile(String fileName, String result) throws IOException { String filePath = "D:\\" + fileName+".txt"; File file = new File(filePath); if (!file.isFile()) { file.createNewFile(); DataOutputStream out = new DataOutputStream(new FileOutputStream( file)); out.writeBytes(result); } } // 讀檔案,返回字串 public static String ReadFile(String path) { File file = new File(path); BufferedReader reader = null; String laststr = ""; try { // System.out.println("以行為單位讀取檔案內容,一次讀一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次讀入一行,直到讀入null為檔案結束 while ((tempString = reader.readLine()) != null) { // 顯示行號 System.out.println("line " + line + ": " + tempString); laststr = laststr + tempString; ++line; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return laststr; } }