java Properties 的一个小问题及简单使用

时间:2022-05-11 14:31:41

由于要对一个小程序进行优化,所以特地学习了下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.xml
java.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 more
there is a exception ,please check it!
null

4.问题定位解决

从 xml 文件读取的 函数 PropertiesFromFile 中
多写了一个
fis.read();

导致xml文件少了一个 字节的数据,故一直出现
InvalidPropertiesFormatException 异常。

5.参考

http://www.yiibai.com/java/util/properties_loadfromxml.html