• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

Java InvalidPropertiesFormatException Example

November 8, 2014 //  by Krishna Srinivasan

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.

Java-InvalidpropertyFormatException-Example

Category: JavaTag: Java Exceptions

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Eclipse Tips : How To Configure MySql In Eclipse
Next Post: Java 8 Date Time API – DateTimeFormatterBuilder Example »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact