Create a Java project and create a “config.properties” file under the root folder (project) not under src.
Project structure:
Config.properties:
Reading Property File:
Create a class file “PropertyFileRead” and paste the below code,
[java]
package com.ngdeveloper.com;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class PropertyFileRead {
public static void main(String[] args) {
Properties propFile = new Properties();
try {
// Loading property file
propFile.load(new FileInputStream("config.properties"));
// reading the value from the property file
String welcomeUser = propFile.get("welcome").toString();
System.out.println(welcomeUser);
// Object welcomeUser = propFile.get("welcome");
// System.out.println(welcomeUser.toString());
} catch (FileNotFoundException fnfe) {
System.out.println("File Not Found : " + fnfe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
}
[/java]
Output:
welcome to Javadomain
Writing to Property File:
Create a class file “PropertyFileWrite“ and paste the below code,
1. Using setProperty method [it calls put method internally]
[java]
package com.ngdeveloper.com;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class propertyFileWrite {
public static void main(String[] args) {
Properties propFile = new Properties();
try {
// Loading property file
propFile.load(new FileInputStream("config.properties"));
// Setting key and value
propFile.setProperty("javadomain_url", "www.ngdeveloper.com");
// Storing the key values
propFile.store(new FileOutputStream("config.properties"),
"using setproperty method");
System.out.println(propFile.get("javadomain_url").toString()
+ " written successfully in config.properties file");
} catch (FileNotFoundException fnfe) {
System.out.println("File Not Found : " + fnfe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
}
[/java]
Output:
www.ngdeveloper.com written successfully in config.properties file
config.properties file – Before the program execution,
config.properties – After the program execution,
2. Using put method:
[java]
package com.ngdeveloper.com;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class propertyFileWrite {
public static void main(String[] args) {
Properties propFile = new Properties();
try {
// Loading property file
propFile.load(new FileInputStream("config.properties"));
// Setting key and value
// propFile.setProperty("javadomain_url", "www.ngdeveloper.com");
propFile.put("javadomain", "Javadomain for java programmers");
// Storing the key values
/*
* propFile.store(new FileOutputStream("config.properties"),
* "using setproperty method");
*/
propFile.store(new FileOutputStream("config.properties"),
"using put method");
System.out.println(propFile.get("javadomain").toString()
+ " written successfully in config.properties file");
} catch (FileNotFoundException fnfe) {
System.out.println("File Not Found : " + fnfe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
}
[/java]
Output:
Javadomain for java programmers written successfully in config.properties file
config.properties file – before the program execution,
config.properties file – after the program execution,
Download the source code:
Recommended Java Books :