First of all you have to initialize the Properties object by loading a ".properties" file.
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
public class PropertyTest {
private static Properties properties;
private static File propertyFile;
public PropertyTest() {
initProperties();
}
private static void initProperties() {
try {
propertyFile = new File("D:/test.properties");
if (!propertyFile.exists()) {
propertyFile.createNewFile();
}
FileInputStream in = new FileInputStream(propertyFile);
properties = new Properties();
properties.load(in);
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.util.Properties;
public class PropertyTest {
private static Properties properties;
private static File propertyFile;
public PropertyTest() {
initProperties();
}
private static void initProperties() {
try {
propertyFile = new File("D:/test.properties");
if (!propertyFile.exists()) {
propertyFile.createNewFile();
}
FileInputStream in = new FileInputStream(propertyFile);
properties = new Properties();
properties.load(in);
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Note the properties.load() method in above code. That method loads the existing properties to properties Object.
Now your properties object is ready to use. You can save or load data now.
Below code add a property called "testProperty" to the file.
private static void addProperty() {
try {
FileOutputStream out = new FileOutputStream(propertyFile);
properties.setProperty("testProperty", "This is test property");
properties.store(out, "This is comment");
out.close();
} catch (Exception ex) {
}
}
try {
FileOutputStream out = new FileOutputStream(propertyFile);
properties.setProperty("testProperty", "This is test property");
properties.store(out, "This is comment");
out.close();
} catch (Exception ex) {
}
}
Note that we have used the properties.store() method to save the value.
In below method you can see how to retrieve data from file.
private static void readProperty() {
String testProperty = (String) properties.get("testProperty");
System.out.println(testProperty);
}
String testProperty = (String) properties.get("testProperty");
System.out.println(testProperty);
}
Note that if the property does not exist in the file it returns null.
That is all.
The following is a full working code.
package testing;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertyTest {
private static Properties properties;
private static File propertyFile;
public PropertyTest() {
initProperties();
}
public static void main(String[] args) {
initProperties();
addProperty();
readProperty();
}
private static void addProperty() {
try {
FileOutputStream out = new FileOutputStream(propertyFile);
properties.setProperty("testProperty", "This is test property");
properties.store(out, "This is comment");
out.close();
} catch (Exception ex) {
}
}
private static void readProperty() {
String testProperty = (String) properties.get("testProperty");
System.out.println(testProperty);
}
private static void initProperties() {
try {
propertyFile = new File("D:/test.properties");
if (!propertyFile.exists()) {
propertyFile.createNewFile();
}
FileInputStream in = new FileInputStream(propertyFile);
properties = new Properties();
properties.load(in);
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertyTest {
private static Properties properties;
private static File propertyFile;
public PropertyTest() {
initProperties();
}
public static void main(String[] args) {
initProperties();
addProperty();
readProperty();
}
private static void addProperty() {
try {
FileOutputStream out = new FileOutputStream(propertyFile);
properties.setProperty("testProperty", "This is test property");
properties.store(out, "This is comment");
out.close();
} catch (Exception ex) {
}
}
private static void readProperty() {
String testProperty = (String) properties.get("testProperty");
System.out.println(testProperty);
}
private static void initProperties() {
try {
propertyFile = new File("D:/test.properties");
if (!propertyFile.exists()) {
propertyFile.createNewFile();
}
FileInputStream in = new FileInputStream(propertyFile);
properties = new Properties();
properties.load(in);
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
No comments:
Post a Comment