[Link].
Properties
examples
CSE219, Computer Science III
Stony Brook University
[Link]
[Link]
Java properties files are used to store project
configuration data or settings.
These can be .properties files or .xml files
Example: [Link]
#Fri March 6 [Link] MYT 2015
database=localhost
dbuser=paul
dbpassword=password
2
(c) Paul Fodor
import [Link];
import [Link]; Load a properties file
import [Link];
import [Link];
public class ReadProperties {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("[Link]");
// load a properties file
[Link](input);
// get the property value and print it out
[Link]([Link]("database"));
[Link]([Link]("dbuser"));
[Link]([Link]("dbpassword"));
} catch (IOException ex) {
[Link]();
} finally {
if (input != null) {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
}
3 }
(c) Paul Fodor
}
import [Link];
import [Link]; Write to properties file
import [Link];
import [Link];
public class WriteProperties {
public static void main(String[] args) {
public static void main(String[] args) {
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("[Link]");
// set the properties value
[Link]("database", "localhost");
[Link]("dbuser", "mkyong");
[Link]("dbpassword", "password");
// save properties to project root folder
[Link](output, null);
} catch (IOException io) {
[Link]();
} finally {
if (output != null) {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
}
4 }
(c) Paul Fodor
}
Also in XML: [Link]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM
"[Link]
<properties>
<entry key="database">localhost</entry>
<entry key="dbuser">paul</entry>
<entry key="dbpassword">password</entry>
</properties>
In Java:
Properties properties = new Properties();
[Link](fileInput);
5
(c) Paul Fodor