DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page Accessing properties file keys/values
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Apr 2008
Default Accessing properties file keys/values - 04-10-2008, 09:39 AM

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

public class PropertyAccess {
public static void main(String[] args) {
Properties prop = new Properties();

try {
prop.load(new FileInputStream(new File("test.properties")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

//returns values if keys are not known
//returns values
Enumeration enumeration = prop.elements();

System.out.println("Printing values");

while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement().toStr ing());
}

//returns keys
Enumeration enumeration2 = prop.keys();

System.out.println("Printing keys");

while (enumeration2.hasMoreElements()) {
System.out.println(enumeration2.nextElement().toSt ring());
}

//if keys are known, directly get the values
System.out.println("Printing values by key names");
System.out.println(prop.getProperty("name"));
System.out.println(prop.getProperty("age"));
System.out.println(prop.getProperty("sex"));
}
}

//output of the program

Printing values
32
Brad pitt
male
Printing keys
age
name
sex
Printing values by key names
Brad pitt
32
male



//properties file

name = Brad pitt
age = 32
sex = male
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing a xml file using javascript on server ashfk Ajax & Javascript 1 04-21-2009 02:38 PM
Problem in accessing help system kindlerm Eclipse 0 10-16-2008 05:31 AM
Error (build.properties does not exist) zohraadnan Eclipse 0 09-22-2008 11:34 AM
Eclipse cursor keys do not work jnoronha Eclipse 1 05-23-2008 04:54 AM
Getting values of another class svds Java 6 02-12-2008 02:31 PM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com