由於要對一個小程式進行最佳化,所以特地學習了下Properties,不過遇到一個問題簡直都要瘋了。。。
** 1.功能描述
**
產生和讀取 屬性xml 檔案 2.原始碼
剛開始的源碼如下:
package test;import java.util.*;import java.io.*;public class PropertiesFileTest { public static void main(String[] args){ //PropertiesToFile(Properties pro,String filePath) Properties pro = new Properties(); pro.setProperty("寬度", "15"); pro.setProperty("寬度", "15"); PropertiesFileTest.PropertiesToFile(pro,"d://config2.xml"); ///* Properties pro2 = PropertiesFileTest.PropertiesFromFile(pro, "d://config2.xml"); System.out.println(pro2.getProperty("From")); //*/ } public static void PropertiesToFile(Properties pro,String filePath){ try{ File target_file = new File(filePath); if(target_file.exists()==false) target_file.createNewFile(); FileOutputStream fos = new FileOutputStream(filePath); //os = new OutputStream(target_file); pro.storeToXML( fos , null); //fos.write(pro.size()); //fos.flush(); } catch(IOException ex){ ex.printStackTrace(); } System.out.println("properties have been written to " + filePath); } public static Properties PropertiesFromFile(Properties pro,String filePath){ try{ File target_file = new File(filePath); if(target_file.exists()==true){ FileInputStream fis =new FileInputStream(filePath); //os = new OutputStream(target_file); fis.read(); //多了一個read 導致檔案出問題,不再是 xml pro.loadFromXML(fis); //貌似不支援UTF-8 //pro.load(fis); pro.list(System.out); System.out.println("file have been read to Properties"); } else { System.out.println("config file doesn't exists !"); } } catch(IOException ex){ ex.printStackTrace(); System.out.println("there is a exception ,please check it!"); } return pro; }}
3.報錯
如下:
properties have been written to d://config2.xmljava.util.InvalidPropertiesFormatException: org.xml.sax.SAXParseException: Content is not allowed in prolog. at java.util.XMLUtils.load(XMLUtils.java:59) at java.util.Properties.loadFromXML(Properties.java:852) at test.PropertiesFileTest.PropertiesFromFile(PropertiesFileTest.java:60) at test.PropertiesFileTest.main(PropertiesFileTest.java:28)Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1038) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283) at java.util.XMLUtils.getLoadingDoc(XMLUtils.java:85) at java.util.XMLUtils.load(XMLUtils.java:57) ... 3 morethere is a exception ,please check it!null
4.問題定位解決
從 xml 檔案讀取的 函數 PropertiesFromFile 中
多寫了一個
fis.read();
導致xml檔案少了一個 位元組的資料,故一直出現
InvalidPropertiesFormatException 異常。 5.參考
http://www.yiibai.com/java/util/properties_loadfromxml.html