Java - Properties
Properties is a subclass of Hashtable.It is used to maintain lists of values in which the key is a String and the value is also a String.
The Properties class is used by many other Java classes. For example, it is the type of object returned by System.getProperties( ) when obtaining environmental values.
Example:
token.properties
ServerAddress=192.168.3.1
ServerPort=80
ThreadCount=5
ReadWriteProperties.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class ReadWriteProperties {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ReadWriteProperties readWriteProperties = new ReadWriteProperties();
try {
readWriteProperties.readProperties();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readProperties() throws FileNotFoundException, IOException{
Properties props = new Properties();
props.load(new FileInputStream("C:\\Development\\Workspace\\token.properties"));
for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); ) {
String name = (String)e.nextElement();
String value = props.getProperty(name);
// now you have name and value
if (name.startsWith("ServerAddress")) {
System.out.println("Server Address is available");
}
}
}
}
Comments
Post a Comment