YangZhiJie 8 年之前
父節點
當前提交
7d74243a8c
共有 1 個文件被更改,包括 26 次插入2 次删除
  1. 26 2
      src/main/java/org/leanwo/management/util/XmlConfigPathService.java

+ 26 - 2
src/main/java/org/leanwo/management/util/XmlConfigPathService.java

@@ -6,6 +6,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -20,7 +21,10 @@ import org.dom4j.Attribute;
 import org.dom4j.Document;
 import org.dom4j.DocumentException;
 import org.dom4j.Element;
+import org.dom4j.io.OutputFormat;
 import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+
 import com.leanwo.management.model.ApplicationSetting;
 import com.leanwo.management.model.XmlConfigPath;
 
@@ -152,9 +156,12 @@ public class XmlConfigPathService {
 		//Collection<String> values = contents.values();
 		String xmlFilePath = setting.getInstallPath() + "/" + xmlConfigPath.getXmlFileName();
 		File file = new File(xmlFilePath);
+		XMLWriter writer = null;// 声明写XML的对象  
 		if (file.exists()) {
 			try {
-				SAXReader reader = new SAXReader();  
+		        SAXReader reader = new SAXReader();  
+		        OutputFormat format = OutputFormat.createPrettyPrint();  
+		        //format.setEncoding("utf-8");// 设置XML文件的编码格式  
 				Document document = reader.read(file);
 				Element root = document.getRootElement();
 				List<String> nodeNames = xmlConfigPath.getNodeNames();
@@ -164,9 +171,25 @@ public class XmlConfigPathService {
 						throw new RuntimeException("在文件:"+xmlFilePath+",中未找到id="+s+"的标签");
 					}
 					dataSource.setText(contents.get(s));
+					 
 				}
+				writer = new XMLWriter(new FileWriter(file), format);  
+                writer.write(document);  
+                
 				} catch (DocumentException e) {
 					e.printStackTrace();
+				} catch (IOException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}finally {
+					if (writer != null){
+						try {
+							writer.close();
+						} catch (IOException e) {
+							// TODO Auto-generated catch block
+							e.printStackTrace();
+						}
+					}
 				}
 			}
 		
@@ -180,9 +203,10 @@ public class XmlConfigPathService {
 				OutputStream out = null;
 				if (file2.exists()) {
 					try {
-						out = new BufferedOutputStream(new FileOutputStream(file2,true));
+						out = new BufferedOutputStream(new FileOutputStream(file2));
 						properties.setProperty("token", token);
 						properties.setProperty("port", port.toString());
+						properties.store(out, "修改");
 					} catch (FileNotFoundException e) {
 						e.printStackTrace();
 					} catch (IOException e) {