java配置文件properties
2015-08-07 15:49
288 查看
首先大家需要了解在什么情况下需要配置文件:在需要更改代码中的某个变量或者某个值,如果直接在代码中更改则需要更改多处,这个时候如果用配置文件则只需要更改一处即可。
下面来看一下怎样写配置文件:
首先在你项目的src文件夹下创建file
然后在floder下面创建file文件 ,以properties格式命名
然后创建一个class用来管理properties
具体代码如下:
static Properties props = new Properties();
static{
try {
props.load(PropertiesManger.class.getClassLoader().getResourceAsStream("config/tank.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperty(String key){
return props.getProperty(key);
}
在每一处以后可能需要改变的值设立变量:如下
//假如添加10辆坦克
int initTankCount = Integer.parseInt(pm.getProperty("initTankCount"));
for(int i=0; i<initTankCount; i++){
tanks.add(new Tank(50+50*(i+1), 100, false, Direction.D, this));
}
最后在配置文件.properties文件中这样写
initTankCount=10
保存运行,是不是成功了
下面来看一下怎样写配置文件:
首先在你项目的src文件夹下创建file
然后在floder下面创建file文件 ,以properties格式命名
然后创建一个class用来管理properties
具体代码如下:
static Properties props = new Properties();
static{
try {
props.load(PropertiesManger.class.getClassLoader().getResourceAsStream("config/tank.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperty(String key){
return props.getProperty(key);
}
在每一处以后可能需要改变的值设立变量:如下
//假如添加10辆坦克
int initTankCount = Integer.parseInt(pm.getProperty("initTankCount"));
for(int i=0; i<initTankCount; i++){
tanks.add(new Tank(50+50*(i+1), 100, false, Direction.D, this));
}
最后在配置文件.properties文件中这样写
initTankCount=10
保存运行,是不是成功了
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统