13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

110Chapter 4Where Am I? Executi<strong>on</strong> C<strong>on</strong>textso that args[0] 3 in the Java code to get the name of the file (see Secti<strong>on</strong> 4.2.1),but since we want to learn about properties, we’ll use the propertymethods here.Now let’s open that property file (Example 4.3).Example 4.3 Dem<strong>on</strong>strating the Properties classimport java.io.*;import java.util.*;public classEnvFileIn{public static voidmain(String [] args)throws IOExcepti<strong>on</strong>{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 EnvFileInNotice the way that we got the value for the envir<strong>on</strong>ment file’s name. Thisform of the getProperty() call provides not <strong>on</strong>ly the name we are lookingup (ENVFILE) but also lets us specify a default value in case the name is notfound 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 anentire file of properties, so you can write out the entire list of properties to the3. In C language, the arg[0] is the command being invoked; not so in Java. In Java, the firstelement of the array is the first argument of the command line (propertyfile in ourexample).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!