package cn.com.tiptop.outsyscheck.util; import IceInternal.Ex; import cn.com.tiptop.outsyscheck.entity.DBXml; import cn.com.tiptop.outsyscheck.entity.Tasks; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JaxbUtil { private static final Logger logger = LoggerFactory.getLogger(JaxbUtil.class); public staticboolean createXml(T datas,String filePath) { try { File f = new File(filePath); if(!f.getParentFile().exists()) { f.getParentFile().mkdirs(); if(!f.exists()) { f.createNewFile(); } } FileWriter fw = new FileWriter(f); JAXBContext context = JAXBContext.newInstance(datas.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 防止文件中文乱码 m.marshal(datas, fw); fw.flush(); fw.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public static boolean createXml(T datas,String filePath,String coding) { try { File f = new File(filePath); if(!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } FileWriter fw = new FileWriter(f); JAXBContext context = JAXBContext.newInstance(datas.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_ENCODING, coding); // 防止文件中文乱码 m.marshal(datas, fw); fw.flush(); fw.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } @SuppressWarnings("unchecked") public static T readXml(String filePath,Class type) { T value = null; InputStream in = null; InputStreamReader read = null; try { JAXBContext context = JAXBContext.newInstance(type); Unmarshaller u = context.createUnmarshaller(); in = new FileInputStream(filePath); read = new InputStreamReader(in,"UTF-8"); value = (T) u.unmarshal(read); }catch(Exception e) { e.printStackTrace(); }finally { try { if(in!=null) { in.close(); } if(read!=null) { read.close(); } }catch(Exception e) { e.printStackTrace(); } } return value; } @SuppressWarnings("unchecked") public static T readXml(String filePath,String coding,Class type) { T value = null; InputStream in = null; InputStreamReader read = null; try { JAXBContext context = JAXBContext.newInstance(type); Unmarshaller u = context.createUnmarshaller(); in = new FileInputStream(filePath); read = new InputStreamReader(in,coding); value = (T) u.unmarshal(read); }catch(Exception e) { e.printStackTrace(); }finally { try { if(in!=null) { in.close(); } if(read!=null) { read.close(); } }catch(Exception e) { e.printStackTrace(); } } return value; } public static void appendTask(String filePath, DBXml dbXml){ Tasks readXml = getTasks(filePath); List list = new ArrayList<>(); if(readXml == null){ readXml = new Tasks(); list.add(dbXml); readXml.setTask(list); createXml(readXml,filePath,"utf-8"); }else { if(readXml.getTask()==null){ list.add(dbXml); }else{ List taskList = readXml.getTask(); for (DBXml tfi:taskList) {//只保存同一个TaskUniqueID if(!tfi.getDbUniqueId().equals(dbXml.getDbUniqueId())){ list.add(tfi); } } list.add(dbXml); } readXml.setTask(list); createXml(readXml,filePath,"utf-8"); } } private static Tasks getTasks(String filePath) { Tasks readXml = null; try{ if(new File(filePath).isFile()){ readXml = readXml(filePath, Tasks.class); } }catch (Exception e){ logger.error("appendTask"+ ExceptionUtil.getException(e)); e.printStackTrace(); } return readXml; } public static void deleteTask(String filePath, String dbUniqueId){ Tasks readXml = getTasks(filePath); if(readXml!=null&&readXml.getTask()!=null){ List list = new ArrayList<>(); List taskList = readXml.getTask(); for (DBXml dbXml:taskList) { if(!dbXml.getDbUniqueId().equals(dbUniqueId)){ list.add(dbXml); } } readXml.setTask(list); createXml(readXml,filePath,"utf-8"); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)