Because of a small program to optimize, so specifically to learn the properties, but encountered a problem is almost crazy ...
* * 1. Functional Description
**
Generate and Read property XML file 2. Source Code
The initial source code is as follows:
Package test;
Import java.util.*;
Import java.io.*; public class Propertiesfiletest {public static void main (string[] args) {//propertiestofile (Properties pro,st
Ring FilePath) Properties Pro = new properties ();
Pro.setproperty ("width", "15");
Pro.setproperty ("width", "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 t
Arget_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 tar
Get_file = new file (FilePath);
if (target_file.exists () ==true) {FileInputStream fis =new fileinputstream (FilePath);
OS = new OutputStream (target_file); Fis.read (); One more read causes file problems, no longer XML pro.loadfromxml (FIS);
Seemingly does not support 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. Error
As follows:
Properties have been written to D://config2.xml Java.util.InvalidPropertiesFormatException:
Org.xml.sax.SAXParseException:Content is isn't 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 C Om.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 are a exception, please check it!
Null
4. Problem positioning Solution
Functions read from XML file Propertiesfromfile
Wrote one more.
Fis.read ();
Causes the XML file to be less than one byte of data, so it always appears
Invalidpropertiesformatexception exception. 5. For reference
Http://www.yiibai.com/java/util/properties_loadfromxml.html