XML, JSON 포맷 및 파일 읽기 및 쓰기 패키지

5614 단어 XMLJson
자세히 보기
package com.org.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
/**
 *@CopyRight:liangjilong
 *@DEMO:midu
 *@Author:jilongliang
 *@Email:[email protected]
 *@Date:2014-1-25
 */
public class HadlerUtil {
	 private static String content = "",line=System.getProperty("line.separator");//     
/** * * @param formPath * @return */ public static Map readConfig(String formPath) { String content = ""; Map map = new HashMap(); FileReader read = null; BufferedReader reader = null; try { read = new FileReader(new File(formPath)); reader = new BufferedReader(read); content = reader.readLine(); while (content != null) { String val[] = content.split("="); if (val.length > 0) { String key = val[0];// , map.put(key, val[1]); } content = reader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null||read!=null) reader.close(); read.close(); } catch (Exception e) { e.printStackTrace(); } } return map; } /** * * @param formPath * @return */ public static String reader(String formPath) { FileReader read = null; BufferedReader reader = null; try { read = new FileReader(new File(formPath)); reader = new BufferedReader(read); StringBuffer buffer = new StringBuffer(""); content = reader.readLine(); while (content != null) { buffer.append(content).append(line); content = reader.readLine(); } return content = buffer.toString();// } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null)reader.close(); if (read != null)read.close(); } catch (Exception e) { e.printStackTrace(); } } return "";// } /** * * @param content * @param toPath * @return */ public static boolean writer(String content, String toPath) { boolean flag = true; try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toPath), "utf-8"));// out.write("
" + content); out.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return flag; } /** * xml , xml * @param document * @param filePath * @return */ public static boolean formatAsXml(Document document) { boolean ret=true; OutputFormat format = OutputFormat.createPrettyPrint();// format.setEncoding("utf-8");// try { /** document new XMLWriter(new FileWriter(new File(filename))); */ XMLWriter writer = new XMLWriter(format); writer.write(document); writer.close(); } catch (IOException e) { e.printStackTrace(); ret=false; } return ret; } /** * xml , xml * @param document * @param filePath * @return */ public static boolean formatAsXml(Document document, String filePath) { boolean ret=true; OutputFormat format = OutputFormat.createPrettyPrint();// format.setEncoding("utf-8");// try { /** document new XMLWriter(new FileWriter(new File(filename))); */ XMLWriter writer = new XMLWriter(new FileWriter(new File(filePath)),format); writer.write(document); writer.close(); } catch (IOException e) { e.printStackTrace(); ret=false; } return ret; } /** * JSON, Json * @param content * @return */ public static String formatAsJSON(String content){ Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jsonPar = new JsonParser(); JsonElement jsonEl = jsonPar.parse(content); String prettyJson = gson.toJson(jsonEl); return prettyJson; } }

좋은 웹페이지 즐겨찾기