By Sergey Skudaev
In this tutorial, you will learn how to use Java properties file to pass parameters to the java application. Four Java code examples show different approaches to the solution of the problem and explain how to write Java code that read parameters from a properties file. Read Start Java Programming tutorial to learn how to configure your PC to be able to run java application.
Copy Greeting2.java source code and paste it in notepad or your favorite editor for plain text. (Do not use MS World). I like TextPad editor. It allows you to use regular expression for search and replace. It displays the text line number. It can compile and run Java Application
Greeting2.java import java.io.*; import java.util.*; public class Greeting2 { String message; // class constructor public Greeting2() { } public void setMessage() { //create an instance of properties class Properties props = new Properties(); //try retrieve data from file try { props.load(new FileInputStream("message.properties")); message = props.getProperty("message"); System.out.println(message); } //catch exception in case properties file does not exist catch(IOException e) { e.printStackTrace(); } } public static void main(String[] args) { //create an instance of greeting2 class Greeting2 gr = new Greeting2(); //call the setMessage() method of the Greeting2 class gr.setMessage(); } } //end of source code file Save the Greeting2.java file in "C:\java\prop" directory Compile the source file Greeting2.java C:\java\prop>javac Greeting2.java Execute Greeting2 Java application C:\java\prop>java Greeting2 java.io.FileNotFoundException: message.properties (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:106) at java.io.FileInputStream. (FileInputStream.java:66) at Greeting2.setMessage(Greeting2.java:37) at Greeting2.main(Greeting2.java:60) C:\java\prop>
Input-Output Exception is thrown because of the message.properties file does not exist.
Let us create it. Open Notepad and type the following text.
message = How are you /
Save the file as message.properties in C:\java\prop directory.
Try to execute Greeting2 application again.
C:\java\prop>java Greeting2
How are you
Now our message is displayed. Open message.properties file and delete the text:
message = How are you.
Save the file.
Try to execute Greeting2 application again.
C:\java\prop>java Greeting2
null
C:\java\prop>
null will display instead of the message
To improve our program let us add default message,
just in case if something will go wrong with getting the message from file
Improved our greeting class
Greeting3.java
import java.io.*; import java.util.*; public class Greeting3 { String message; public Greeting3() { } public void setMessage() { //create an instance of properties class Properties props = new Properties(); //try retrieve data from file //catch exception in case properties file does not exist try { props.load(new FileInputStream("message.properties")); message = props.getProperty("message"); //If value of message variable is null assign default value "Hello World" if(message==null) { message=new String("Hello World!"); } System.out.println(message); } //catch exception in case properties file does not exist catch(IOException e) { e.printStackTrace(); } } public static void main(String[] args) { //create an instance of Greeting3 class Greeting3 gr = new Greeting3(); //call the setMessage() method of the Greeting3 class gr.setMessage(); } }
Compile the source file Greeting3.java C:\java\prop>javac Greeting3.java Execute Greeting3 Java application C:\java\prop>java Greeting3 Hello World! Open message.properties file and enter text message = How are you Save the file Execute Greeting3 Java application again "How are you" message displays C:\java\prop>java Greeting3 How are you |
Let us improve the application. Set default message in class constractor.
import java.io.*; import java.util.*; public class Greeting4 { String message; public Greeting4() { message= new String("Hello World"); } public void setMessage() { //create an instance of properties class Properties props = new Properties(); //try retrieve data from file try { props.load(new FileInputStream("message.properties")); // assign value to message variable only if it is not null if(props.getProperty("message") !=null) { message = props.getProperty("message"); } System.out.println(message); } //catch exception in case properties file does not exist catch(IOException e) { System.out.println(message); } } public static void main(String[] args) { //create an instance of Greeting4 class Greeting4 gr = new Greeting4(); //call the setMessage() method of the Greeting4 class gr.setMessage(); } }
Compile the source file Greeting4.java
C:\java\prop>javac Greeting4.java
Execute Greeting4 Java application
C:\prop\final>java Greeting4
How are you
Open message.properties file and delete text "message = How are you."
Download the complete source code for a responsive website with user registration and authentication.
Execute Greeting4 Java application again
C:\prop\final>java Greeting4
Hello World
Delete message.properties file
Execute Greeting4 Java application again
C:\prop\final>java Greeting4
Hello World
Hello World default message will be displayed in both cases.
Application output.