天天看点

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