Tuesday, August 30, 2016

property

 package com.hr.automation.utilities;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileNotFoundException;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.net.InetAddress;  
 import java.net.UnknownHostException;  
 import java.util.Properties;  
 public class Property {  
      Properties props=new Properties();  
      String strFileName;  
      String strValue;  
      public String getProperty(String strKey) {  
           try{  
            strValue=props.getProperty(strKey);  
           }  
            catch (Exception e) {  
                System.out.println(e);  
           }  
           return strValue;  
      }  
      public void setProperty(String strKey,String strValue) throws Throwable {  
           try{  
                props.setProperty(strKey, strValue);  
                props.store(new FileOutputStream(strFileName),null);  
           }  
           catch (Exception e) {  
                System.out.println(e);  
           }  
      }  
      public void removeProperty(String strKey){  
           try{  
                props.remove(strKey);  
                props.store(new FileOutputStream(strFileName),null);  
                }  
                 catch (Exception e) {  
                     System.out.println(e);  
                }   
      }  
      public Property(String strFileName){  
           this.strFileName=strFileName;  
           System.out.println(strFileName);  
           File f = new File(strFileName);  
            if(f.exists()){  
            FileInputStream in = null;  
           try {  
                in = new FileInputStream(f);  
           } catch (FileNotFoundException e1) {  
                e1.printStackTrace();  
           }  
                 try {  
                     props.load(in);  
                } catch (IOException e) {  
                     e.printStackTrace();  
                }  
                 try {  
                     in.close();  
                } catch (IOException e) {  
                     e.printStackTrace();  
                }  
            }  
            else  
            System.out.println("File not found!");  
      }  
      // return environmental details  
      public static String getHostName() throws UnknownHostException{  
           InetAddress addr = InetAddress.getLocalHost();  
           //byte[] ipAddr = addr.getAddress();  
            String hostname = addr.getHostName();        
           return hostname;  
      }  
 }  

No comments:

Post a Comment