Java Reference
In-Depth Information
so that args[0] 3 in the Java code to get the name of the file (see Section 4.2.1),
but since we want to learn about properties, we'll use the property
methods here.
Now let's open that property file (Example 4.3).
Example 4.3 Demonstrating the Properties class
import java.io.*;
import java.util.*;
public class
EnvFileIn
{
public static void
main(String [] args)
throws IOException
{
String envfile = System.getProperty("ENVFILE", ".envfile");
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(envfile));
Properties prop = new Properties();
prop.load(bis);
bis.close();
prop.list(System.out); // dumps the whole list to System.out
} // main
} // class EnvFileIn
Notice the way that we got the value for the environment file's name. This
form of the getProperty() call provides not only the name we are looking
up ( ENVFILE ) but also lets us specify a default value in case the name is not
found in the properties list. Here our default value is .envfile .
Just as it was a simple matter of using the load() method to read up an
entire file of properties, so you can write out the entire list of properties to the
3. In C language, the arg[0] is the command being invoked; not so in Java. In Java, the first
element of the array is the first argument of the command line ( propertyfile in our
example).
Search WWH ::




Custom Search