JAVA操作屬性文件,可進行讀 寫 更改


  
JAVA 操作屬性文件
/*
操作屬性文件,可以為我們的程序帶來更方便的移植性,下面是一個示例,可以讀、寫、更改屬性
讀采用了兩種方式,一種是采用 Properties 類,另外一種是采用資源綁定類 ResourceBundle 類,
下面是源程序,里面有詳細的注釋:
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.ResourceBundle;
/**
  * 對屬性文件( xx.properties )的操作
  * 注:屬性文件一定要放在當前工程的根目錄下,也就是放在與 src 目錄在同一個目錄下(我的 JDevelop
  * 是這樣的)
  */
public class OperatePropertiesFile {
    public OperatePropertiesFile() {
    }
    /**
     * 采用 Properties 類取得屬性文件對應值
     * @param propertiesFileName properties 文件名,如 a.properties
     * @param propertyName 屬性名
     * @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 類取得屬性文件對應值,這個只能夠讀取,不可以更改及寫新的屬性
     * @param propertiesFileNameWithoutPostfix properties 文件名,不帶后綴
     * @param propertyName 屬性名
     * @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;
    }
    /**
     * 更改屬性文件的值,如果對應的屬性不存在,則自動增加該屬性
     * @param propertiesFileName properties 文件名,如 a.properties
     * @param propertyName 屬性名
     * @param propertyValue 將屬性名更改成該屬性值
     * @return 是否操作成功
     */
    private boolean 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;
    }
    public static void main(String[] args) {
        OperatePropertiesFile operatePropertiesFile = new OperatePropertiesFile();
        operatePropertiesFile.changeValueByPropertyName( "db.properties" , "DBLocation" , "D://Palfinger//palfinger.mdb" );
    }
}
假如有一個屬性文件 db.properties如下:  
DBLocation=D/://Palfinger//palfinger.mdb

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM