java中經常用到的讀寫設定檔的資訊properties屬性的寫入,讀取例子

來源:互聯網
上載者:User

package com.util;
import java.io.*;
import java.util.*;
/**
 *
 * @author zhaizhanpo
 *
 *
 */

public class ReadWriteProUtil {
   
 
 /**
  * 根據key讀取value
  * 在工程中取得相對路徑的方法 :this.getClass().getResource("工程路徑").getPath();
  * @param filePath
  * @param key
  * @return
  */
    public static String readValue(String filePath, String key){
        Properties props = new Properties();
        FileInputStream in=null;
        try{
            in = new FileInputStream(filePath);
            props.load(in);
            String value = props.getProperty(key);
            return value;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally{
         try {
    in.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
        }
    }
   
    /**
     * 讀取properties的全部資訊
     * @param filePath 檔案的路徑
     */
    public static void readProperties(String filePath){
        Properties props = new Properties();
        InputStream in=null;
        try{
            in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            Enumeration en = props.propertyNames();
            while(en.hasMoreElements()){
                String key = (String)en.nextElement();
                String property = props.getProperty(key);
                System.out.println(key + " : " + property);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
         try {
    in.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
        }
    }
   
    /**
     * 寫入properties資訊
     * @param filePath 要寫入的檔案的路徑
     * @param parameterName 要改變的值得件
     * @param parameterValue 要改成的內容
     */
   
    public static void writeProperties(String filePath, String parameterName, String parameterValue){
        Properties props = new Properties();
        OutputStream fos=null;
        InputStream fis =null;
        try{
            fis = new FileInputStream(filePath);
            //從輸入資料流中讀取屬性列表(鍵和元素對)
            props.load(fis);
             fis.close();
            fos = new FileOutputStream(filePath);
            props.setProperty(parameterName, parameterValue);
            //以適合使用load方法載入到Properties表中的格式,將此Properties表中的屬性列表(鍵和元素對)寫入輸出資料流

            //載入額外的內容
            String otherContent="making people zhaizhanpo";
            props.store(fos,otherContent);
        }catch(IOException e){
            e.printStackTrace();
        }finally{
         try {
    fis.close();
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }       
        }
    }

    public static void main(String[] args) {
     // example 更改使用者名稱稱
     String filepath="D://Test//src//pro.txt";
     ReadWriteProUtil.writeProperties(filepath,"userName","000");
     //輸出更改後的資訊。
        System.out.println(ReadWriteProUtil.readValue(filepath,"userName"));
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.