![]() |
| | Home | Ink Cartridges | Collectibles/Toys | Computer Books | Geeks Electronics | EMagazines | AboutUs | |
Canon PowerShot A590 8.0 MP Digital Camera - Only $139.95!
HP Store search box: Computers, Electronics, Hardware |
Canon Instant Rebates at Calumet Photographic! Save up to $125 instantly on select Canon Lenses and Flashes! Expires 1.17.09
![]()
![]()
![]()
![]()
![]()
![]()
By Sergey Skudaev
In this tutorial you will learn how to use Java properties file to pass parameters to java application. There are four Java code examples that show different approaches to problem solution and explain how to write Java code that read parameters from 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).
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.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at Greeting2.setMessage(Greeting2.java:37)
at Greeting2.main(Greeting2.java:60)
C:\java\prop>
Input Output Exeption is thrown because message.proporties file does not exist.
Let us create it. Open Notepad and type the following text
message = How are you
Save 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 message
To improve our program let us add default message,
just in case if something will go wrong with getting 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 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 |
720p High-Definition LCD TV, was $699.00 and is now $459.00 use code: AF1008 Black & Decker Bullseye Laser Level was $124.95 is now $37.95 use code: AF1008 Philips 20 Pack "AAA" Batteries was $9.99 is now $3.49 use code: AF1008 Philips 20 Pack "AA" Batteries was 9.99 is now $3.49 use code: AF1008 Philips 7" Portable Divx DVD Player with Ipod Dock was $149.99 is now $94.95 use code: AF1008 DECT 6.0 Cordless Phone Bundle with Answering Machine was $79.99 is now $37.95 use code: AF1008 6' Acoustic Research Performance Series HDMI Cable was $69.95 and is now $17.95 use code: AF1008 12' Acoustic Research Performance Series HDMI Cable was $89.95 and is now $19.95 use code: AF1008 |
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.proporties file and delete text "message = How are you"
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.
If you have any question or comments please post them in forum
Password Keeper VB Application
Seagate FreeAgent Pro 750GB Ext
FW/USB/eSATA Hard Drive
32" Vizio Vs42LF 720p Widescreen
HDTV LCD TV - $479.99
Seagate 750GB Hard Drive
only $98 + Free external case with code FREECASE @ Geeks.com
Compaq Pentium Dual-Core 1.6GHz 1GB 160GB DVD±RW Vista
Please rate the tutorial
Good Deal...
HP Photosmart Pro B8350 Photo Printer |