天天看點

Property檔案讀取

參考:民團司令的部落格,網址:http://blog.csdn.net/dotnetstudio/article/details/47185569

在同一個項目中,同一個包下放置類PropertyParser.java和config.properties

PropertyParser.java中為:

package cn.cmri.conf;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.util.Properties;

public class PropertyParser {

    public static String getIp(){

        Properties properties = new Properties();

        String name = PropertyParser.class.getResource("").getPath();

        String path = name + "config.properties";

        File file = new File(path);

        System.out.println(path);

        String ip="";

        if (file.exists()) {

            System.out.println("存在檔案");

            try {

                InputStream fis = new FileInputStream(file);

                properties.load(fis);

                ip=properties.getProperty("ip");

                fis.close();

            } catch (Exception e) {

                System.out.println("存在檔案,但系統出現異常");

                e.printStackTrace();

            }

        }else{

            System.out.println("不存在檔案");

        }

        return ip;

    }

    public static void main(String[] args) {

        getIp();

    }

}

(2)config.properties

ip=121.1.1.1