For reading and writing data into properties file I am using Properties class in this post.
Create a file name with extension .properties like I have created “File.properties” below file:
Create a file name with extension .properties like I have created “File.properties” below file:
Reading Data: Below code read data from above file using key and file name as an arguments.
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class FileReader {
public
String readData(String key, String fileName) {
String value = "";
try {
Properties
properties = new Properties();
File
file = new
File("File.properties");
if
(file.exists()) {
properties.load(new
FileInputStream(file));
value
= properties.getProperty(key);
}
}
catch (Exception e) {
System.out.println(e);
}
return value;
}
public
static void main(String []str){
FileReader fileReader = new
FileReader();
String
name = fileReader.readData("Name", "fileName");
System.out.println("Name
:"+name);
String
url = fileReader.readData("URL", "fileName");
System.out.println("URL
:"+url);
}
}
If you run above file you will get Name and Url values and print on console