java读取配置文件Properties
2017-09-27 14:09
316 查看
Properties介绍博客:http://www.cnblogs.com/bakari/p/3562244.html
代码实例:
文件路径:
![](https://img-blog.csdn.net/20170927151253397?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpeGluXzM2NTg2NTY0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
代码实例:
package com.self.cn.weather.env; import org.apache.log4j.Logger; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ServerEnvironment { private static String apiUser = null; private static String apiKey = null; private static String apiGateway = null; private static String sessionUrl = null; private static Properties weatherMap = null; static { Properties properties = new Properties(); InputStream input = null; ClassLoader classLoader = ServerEnvironment.class.getClassLoader(); try { //根目录路径 System.out.println(ServerEnvironment.class.getResource("").getPath()); input = new FileInputStream(classLoader.getResource("configWeather.properties").getFile()); //input = new FileInputStream(classLoader.getResource("com/self/cn/weather/env/configWeather2.properties").getFile()); //获取class下指定文件夹中的配置文件 //方式一 //input = ServerEnvironment.class.getResourceAsStream("/properties/jdbc.properties"); //方式为 //input = new FileInputStream(classLoader.getResource("properties/jdbc.properties").getFile()); properties.load(input); input.close(); } catch (IOException e) { Logger.getLogger(ServerEnvironment.class).fatal("No Config file!", e); } ServerEnvironment.apiUser = properties.getProperty("api.user"); ServerEnvironment.apiKey = properties.getProperty("api.key"); ServerEnvironment.apiGateway = properties.getProperty("api.gateway"); ServerEnvironment.apiGateway = properties.getProperty("api.gateway"); ServerEnvironment.sessionUrl = properties.getProperty("id5.servlet.session"); InputStream mapInput = null; try { mapInput = new FileInputStream(classLoader.getResource("weather_map.properties").getFile()); weatherMap = new Properties(); weatherMap.load(mapInput); mapInput.close(); } catch (IOException e) { Logger.getLogger(ServerEnvironment.class).fatal("No Weather Map file!", e); } } public static String getApiUser() { return apiUser; } public static String getApiKey() { return apiKey; } public static String getApiGateway() { return apiGateway; } public static String getSessionUrl() { return sessionUrl; } public static Properties getWeatherMap() { return weatherMap; } public static void main(String[] args) { System.out.println(ServerEnvironment.getApiUser()); } }
文件路径:
二 方法类
相关文章推荐
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息
- java 读取properties配置文件
- java 读取配置文件(properties)
- java中经常用到的读写配置文件的信息properties属性的写入,读取例子
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息
- java读取 .properties配置文件
- Java Properties 类读取配置文件信息
- java读取properties配置文件
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息
- java 读取配置文件(properties)
- 利用Java的Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息,Java properties文件的操作(zz)
- java读取.properties配置文件 的几种方式
- 使用java读取配置文件(ini、properties)
- java 读取 properties 配置文件 备忘
- Java Properties 类读取配置文件信息