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.

4.5 The Runtime Class111screen with the list() method. The argument to list() is either aPrintStream or a PrintWriter. System.out is a PrintStream, so thatwill work.The format of the properties file is name=value. But it is also possible toput comments in a properties file. Any line beginning with a “#” is ignored.Try it.It’s also easy to (re)write a file of properties with the store() method.The parameters are an OutputStream and a String; the latter will serve as alabel for the parameters, written to an opening comment in the properties file.If your program needs to examine the list of property names, you can getan Enumerator of the entire list via the propertyNames() method. ModifyExample 4.3 to replace the list() call with a do-it-yourself versi<strong>on</strong> that usesthe Enumerator returned from propertyNames() to list all the names andvalues. Hint: Use getProperty() <strong>on</strong> each name retrieved via the enumerati<strong>on</strong>.The Java Properties class extends the java.util.Hashtable class.This means, in part, that all the other Hashtable methods are available to aProperties class. Methods such as c<strong>on</strong>tainsKey() or c<strong>on</strong>tainsValue()can be helpful, as can isEmpty(). One cauti<strong>on</strong>, though. You should usesetProperty() if you want to add values to Properties, rather thanthe Hashtable’s put() method. They do largely the same thing, butsetProperty() enforces that its parameters are Strings. This is important ifyou want to write out the properties to a file, as it’s meant for Strings <strong>on</strong>ly.4.5THE Runtime CLASSLet’s discuss <strong>on</strong>e last way to get to the underlying (<strong>Linux</strong>) system informati<strong>on</strong>.Be warned, though, that this is the least portable approach of all we havementi<strong>on</strong>ed.4.5.1 exec()Familiar to C/C++ programmers, the exec() call in the Java Runtime classdoes much the same thing. It gives you a way to start another program outsideof the current Java Virtual Machine. In doing so, you can c<strong>on</strong>nect to its standardin/out/err and either drive it by writing to its standard in, or read itsresults from its standard out. (Yes, that’s correct—we write to its input andread from its output. If that sounds wr<strong>on</strong>g, think it through. Our Java code is

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

Saved successfully!

Ooh no, something went wrong!