【Android实战】json文件读取并将数据写入文件

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;
	}

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值