Reading and Writing Configuration Properties
Java programs are more versatile when they can be configured using command-line arguments, as you have demonstrated in several applications created in preceding hours. The java.util
package includes a class, Properties
, that enables configuration settings to be loaded from another source: a text file.
The file can be read like other file sources in Java:
• Create a File
object that represents the file.
• Create a FileInputStream
object from that File object.
• Call load()
to retrieve the properties from that input stream.
A properties file has a set of property names followed by an equal sign (=
) and their values. Here’s an example:
username=leptonlastCommand=open databasewindowSize=32
Each property ...
Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.