1. 程式人生 > >java讀取json檔案並轉換為String

java讀取json檔案並轉換為String

import java.io.*;


public class Output {
	//測試
	public static void main(String[] args){
		String json = "null";
		try {
			json = readJsonData("I:\\History_Project\\echarts\\life-expectancy.json");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.
out.println(json); } public static String readJsonData(String pactFile) throws IOException { // 讀取檔案資料 //System.out.println("讀取檔案資料util"); StringBuffer strbuffer = new StringBuffer(); File myFile = new File(pactFile);//"D:"+File.separatorChar+"DStores.json" if (!myFile.exists()) { System.
err.println("Can't Find " + pactFile); } try { FileInputStream fis = new FileInputStream(pactFile); InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8"); BufferedReader in = new BufferedReader(inputStreamReader); String str; while ((str = in.readLine()) !=
null) { strbuffer.append(str); //new String(str,"UTF-8") } in.close(); } catch (IOException e) { e.getStackTrace(); } //System.out.println("讀取檔案結束util"); return strbuffer.toString(); } }