Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a config file

Status
Not open for further replies.
Jun 19, 2001
86
US
I am writing an application that needs to read a config file. I understand how to read the file using File, FileReader, and BufferedReader. My config file contains information in the following format:

user = aaron

What would be the best way to get the value of user? I have considered using tokenizer to split on the spaces. I just wanted to be sure this was standard practice in java. If there is a package that will process a config file information would be greatly appreciated.

Nathan
 
Hi Nathan,

you should try using
Code:
java.util.Properties
to load your config file:

If you create a new
Code:
Properties
object and then use the
Code:
load()
function to load in the file from the
Code:
FileInputStream
, you can then use the
Code:
getProperty()
function to retrieve the values you're looking for (and even specify a default value if none is present in the config file)
 
Compining java.util.Properties and an InputStream will allow you to retrieve name=value pairs ...

try {
Properties p = new Properties();
InputStream in = new FileInputStream("config.txt");
p.load(in);
} catch (IOException ioe) {
//handle error ...
}

System.out.println(props.get("user"));
 
Sorry greeneka - missed your post whilst typing !!!
Also, I missed its not get() but getProperty() ....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top