java Properties 的一個小問題及簡單使用

來源:互聯網
上載者:User

由於要對一個小程式進行最佳化,所以特地學習了下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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.