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:
    
    
                         
                         
    
    
    
                         
When you run you will see that values are updated and new key values added for the “NewName” key
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
Writing Data: below code write data into mentioned file if key is already exist then it update value and if key not exist it add new key value.
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
void addData(String key, String val, String fileName) {
           try {
                         File
file = new File("File.properties");                                                
                         Properties
properties = new Properties();
                         properties.load(new
FileInputStream(file));
                         FileOutputStream
obj = new FileOutputStream(file);
                         properties.setProperty(key,
val);
                         properties.store(obj,
"Update data into file ");
           } catch (IOException ex) {
                         ex.printStackTrace();
           }
    }
    public
static void main(String []str){
              FileReader fileReader = new
FileReader();
              fileReader.addData("Name",
"Automatn", "fileName");
              fileReader.addData("FName",
"Testing", "fileName");   
    } 
When you run you will see that values are updated and new key values added for the “NewName” key
how do I read a excel file data using properties file?
ReplyDeletethis is becoz i don't want to hard code the path of the excel file