java项目读取properties的几种方式

来源:互联网 发布:淘宝秋冬男旅游鞋 编辑:程序博客网 时间:2024/06/05 09:41

从网上搜集了java项目读取properties的几种方式:



1. IO文件流

public static void load1() throws Exception{    //文件真实路径        String fileName="E:/eclipse_workspace/LogToOracle/config/redisManage.properties";        Properties p=new Properties();        InputStream is=new FileInputStream(new File(fileName));        p.load(is);        System.out.println(p);    }


2. 相对路径

//相对路径    public static void load2() throws Exception{        Properties p=new Properties();        //InputStream is=ClassLoader.getSystemResourceAsStream("redisManage.properties");     InputStream is=Thread.currentThread().getContextClassLoader().getSystemResourceAsStream("redisManage.properties");        p.load(is);        System.out.println(p);    }        public static void load2_1() throws Exception{        Properties p=new Properties();        InputStream is=SourceLoader.class.getClassLoader().getSystemResourceAsStream("redisManage.properties");        p.load(is);        System.out.println(p);    }


3. 获取src(类包)下的properties

//相对于类路径  properties文件和java放在一起    public static void load3() throws Exception{        Properties p=new Properties();        //InputStream is=ClassLoader.getSystemResourceAsStream("redisManage.properties");        InputStream is=SourceLoader.class.getResourceAsStream("redisManage.properties");        p.load(is);        System.out.println(p);    }


阅读全文
0 0
原创粉丝点击