JAVA 작업 속성 파일, 읽 기, 쓰기, 변경 가능

3571 단어
   
JAVA      
/*
      ,                 ,       ,   、 、    
        ,     Properties ,            ResourceBundle ,
      ,        :
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.ResourceBundle;
/**
 *     (xx.properties)   
 * :                  ,      src         (  JDevelop
 *    )
 */
publicclass OperatePropertiesFile {
    public OperatePropertiesFile() {
    }
    /**
     *  Properties          
     *@parampropertiesFileNameproperties   , a.properties
     *@parampropertyName   
     *@return           ,     ""
     */
    private String getValueByPropertyName(String propertiesFileName,String propertyName) {
        String s="";
        Properties p=new Properties();//         
        FileInputStream in;
        try {
            //propertiesFileName test.properties
            in = new FileInputStream(propertiesFileName);//           
            p.load(in);//                  
            in.close();//     
            s=p.getProperty(propertyName);//        
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }
    /**
     *  ResourceBundel          ,       ,           
     *@parampropertiesFileNameWithoutPostfixproperties   ,    
     *@parampropertyName   
     *@return           ,     ""
     */
    private String getValueByPropertyName_(String propertiesFileNameWithoutPostfix,String propertyName) {
        String s="";
        //      test.properties,   propertiesFileNameWithoutPostfix    test
        ResourceBundle bundel = ResourceBundle.getBundle(propertiesFileNameWithoutPostfix);
        s=bundel.getString(propertyName);
        return s;
    }
    /**
     *        ,          ,        
     *@parampropertiesFileNameproperties   , a.properties
     *@parampropertyName   
     *@parampropertyValue           
     *@return      
     */
    privateboolean changeValueByPropertyName(String propertiesFileName,String propertyName,String propertyValue) {
        boolean writeOK=true;
        Properties p=new Properties();
        FileInputStream in;
        try {
            in = new FileInputStream(propertiesFileName);
            p.load(in);//
            in.close();
            p.setProperty(propertyName,propertyValue);//     ,         
            //p.setProperty("testProperty","testPropertyValue");
            FileOutputStream out=new FileOutputStream(propertiesFileName);//   
            p.store(out,"Just Test");//     ,     ,       ""   
            out.flush();//    ,    
            out.close();//     
        } catch (Exception e) {
            e.printStackTrace();
        }
        return writeOK;
    }
    publicstaticvoid main(String[] args) {
        OperatePropertiesFile operatePropertiesFile = new OperatePropertiesFile();
        operatePropertiesFile.changeValueByPropertyName("db.properties","DBLocation","D://Palfinger//palfinger.mdb");
    }
}
         db.properties  :  
DBLocation=D/://Palfinger//palfinger.mdb

좋은 웹페이지 즐겨찾기