java读取.properties文件
在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件。
案例:
1:config.properties文件
name=\u843D\u82B1\u6709\u610F
str=\u6D41\u6C34\u65E0\u60C5
2:PropertiesUtil 类
package com.gcs.util; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class PropertiesUtil {
private static String default_properties = "文件名.properties";
private static Properties prop;
static {
prop = new Properties();
try {
InputStream is = new BufferedInputStream(new FileInputStream(getPath() + default_properties));
prop.load(is);
} catch (Exception e) {
e.printStackTrace();
}
} public static String getProperty(String key) {
return prop.getProperty(key);
} public static String getProperty(String key, String defaultValue) {
String value = prop.getProperty(key);
if (value == null)
return defaultValue;
return value;
} public static boolean getBooleanProperty(String name, boolean defaultValue) {
String value = prop.getProperty(name);
if (value == null) return defaultValue;
return (new Boolean(value)).booleanValue();
} public static int getIntProperty(String name) {
return getIntProperty(name, 0);
} public static int getIntProperty(String name, int defaultValue) {
String value = prop.getProperty(name);
if (value == null) return defaultValue;
return (new Integer(value)).intValue();
} public static String getPath() {
return Thread.currentThread().getContextClassLoader().getResource("").getPath();
} /**
* 读取指定properties中的值
* @param properties 文件名
* @param name 要读取的属性
* @return
*/
private String readProper(String properties, String name) {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(properties);
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
return p.getProperty(name);
} public static void main(String[] args) {
PropertiesUtil propertiesUtil = new PropertiesUtil();
String name = PropertiesUtil.getProperty("name");
String str = propertiesUtil.readProper("config.properties","str");
System.out.println("name=="+name+","+"str=="+str);
}
}
java读取.properties文件的更多相关文章
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- java读取properties文件时候要注意的地方
java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
- java基础—java读取properties文件
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- Java基础学习总结(15)——java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- Java 读取Properties文件时应注意的路径问题
1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题: InputStream in = this.getClass().getRe ...
随机推荐
- 如何利用sendmail发送外部邮件?
在写监控脚本时,为了更好的监控服务器性能,如磁盘空间.系统负载等,有必要在系统出现瓶颈时,及时向管理员进行报告.在这里通常采用邮件报警,同时,邮件设置为收到邮件,即向指定手机号码发送短信.这样可以实现 ...
- 设计数据库字段或者java中使用boolean型时需谨慎
boolean型变量只有两个值 false和true,我们在设计数据库字段时或者定义java变量时会使用boolean,通常情况下开关类的变量使用无可非议,但请一定要考虑到扩展性. 使用前请仔细考虑一 ...
- 记录一则ORA-00054,ORA-00031解决过程
生产环境:AIX 5.3 + Oracle 10.2.0.5 任务要求:普通表改造分区表,历史数据不要 这个需求很简单: pl/sql导出建表语句,依次修改成分区的建表语句,注意将索引修改成本地索 ...
- struts2学习笔记--OGNL表达式1
struts2标签库主要使用的是OGNL语言,类似于El表达式,但是强大得多,它是一种操作对象属性的表达式语言,OGNL有自己的优点: 能够访问对象的方法,如list.size(); 能够访问静态属性 ...
- Cesium应用篇:1快速搭建
范例中所有范例可以在Github中搜索:ExamplesforCesium Cesium ['siːzɪəm]是一款开源的JavaScript开源库,开发者通过Cesium,实现无插件的创建三维球和二 ...
- Latex学习笔记-序
在写论文的过程中自学了$\LaTeX$, 想总结一下使用心得.刚开始整理了一下发现非常之乱,不知这么多零碎的知识该如何整理的有条理点,至少以后用到还能翻翻自己博客.这次写作论文图省劲用了IEEE的模板 ...
- 分享一个UI与业务逻辑分层的框架(三)
序言 前两篇讲解了UIMediator框架的使用及具体原理代码.本篇讲述MediatorManager的实现代码及展望. MediatorManager MediatorManager的作用有两点: ...
- Xamarin.Android之布局文件智能提示问题
一.前言 看到有人问关于xamarin.android的布局没智能提示问题(VS 2015),当然,写布局这东西没提示这是一件相对痛苦的事 ,所以这里就提供一个解决的方案! 二.解决方案 想要智能提示 ...
- GitHub-版本控制
GitHub的使用:注册,登录,邮箱激活,创建第一个repository.如果branch是master,修改,提交,master直接改变. 稳妥起见创建另一个branch,修改,提交,再发出pull ...
- C#多线程编程
一.使用线程的理由 1.可以使用线程将代码同其他代码隔离,提高应用程序的可靠性. 2.可以使用线程来简化编码. 3.可以使用线程来实现并发执行. 二.基本知识 1.进程与线程:进程作为操作系统执行程序 ...