When we are using the collection of properties, if the input does not conform to the valid XML document type with the appropriate properties specification at that time an exception is thrown called InvalidPropertiesFormatException, to indicate operation could not be completed. This example explains about that exception.
Java InvalidPropertiesFormatException Class Declaration
public class InvalidPropertiesFormatException extends IOException
In the above declaration, the class InvalidPropertiesFormatException is the sub class of the IOException which handles the InvalidPropertiesFormatException.
Java InvalidPropertiesFormatException Constructors
Constructor | Description |
---|---|
InvalidPropertiesFormatException (String message) | It creates a constructor called InvalidPropertiesFormatException for the specified parameter message of type string. The parameter message is saved which is invoked later by using the method Throwable.getmessage (). |
InvalidPropertiesFormatException (String cause) | It creates a constructor called InvalidPropertiesFormatException for the specified parameter cause of type string. The parameter cause is saved which is invoked later by using the method Throwable.getcause (). |
Java InvalidPropertiesFormatException Example
Below Example illustrates InvalidPropertiesFormatException which is thrown when you try to load a set of properties from an XML file. In the below example we are using the DTD file definitions and an XML that must be complaint with the DTD file. If there is any deviation, there should be an exception.
Properties.dtd Example
Below is the DTD for XML file to be loaded and saved it with the name properties.dtd.
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT properties (comment?, entry*) > <!ATTLIST properties version CDATA #FIXED "1.0"> <!ELEMENT comment (#PCDATA) > <!ELEMENT entry (#PCDATA) > <!ATTLIST entry key CDATA #REQUIRED>
sampleproperties.xml Example
Following is a XML file saved with the name sampleproperties.xml. The XML file must contain the same element as defined in the DTD.
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Information</comment> <entry key=”name”>Tanmay Patil</entry> <entry key="company">Tutorials Point</entry> <entry key="phone"> (0111) 123-456</ entry > </properties>
NOTE:<!DOCTYPE properties SYSTEM “http://java.sun.com/dtd/properties.dtd”> line has to be included in the prolog section of your XML file, as it is not allowed to use a DTD other than this while calling the java.util.Properties.loadFromXML ().
Following is a example shows how to load a set of properties from an XML file.
package javabeat.net.corejava; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.InvalidPropertiesFormatException; import java.util.Properties; public class Example_InvalidFormat { public static void main (String args []) { Properties p1 = new Properties (); String property_file = "properties.dtd"; try { System.out.println ("Before properties load: " + p1); prop.loadFromXML (new FileInputStream (property_file)); System.out.println ("After properties load : " + p1); } catch (InvalidPropertiesFormatException e) { e.printStackTrace (); } catch (FileNotFoundException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } } }
- String property_file = “properties.dtd”; line specifies the XML file name which you want to load.
- prop.loadFromXML (new FileInputStream (property_file)); lines reads the specified XML file name. When it encounters, the file name and finds that name specified is not the XML file instead is a DTD file. Then, main throws an InvalidPropertiesFormatException exception.
If you run the above example, you would get the exception thrown like in the below screenshot.