读取properties文件
假设项目名称为myproject
public class UtilConfig {
private static final Properties prop;
static {
prop = new Properties();
try {
String PROPERTIES_FILE_SYSTEM_VAR = "moral.website.properties";
String propertiesFile = System.getProperty(PROPERTIES_FILE_SYSTEM_VAR);
if ( propertiesFile == null ) {
propertiesFile = "/setup/myproject.properties";
}
prop.load(UtilConfig.class.getResourceAsStream(propertiesFile));
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getInt(String key) {
return getInt(key, 100);
}
public static int getInt(String key, int defaultValue) {
try {
return Integer.parseInt(prop.getProperty(key, String.valueOf(defaultValue)));
} catch (Exception e) {
return defaultValue;
}
}
public static String getString(String key, String defaultValue) {
return prop.getProperty(key, defaultValue);
}
public static String getString(String key) {
return prop.getProperty(key);
}
public static long getLong(String key) {
return getLong(key, 1000L);
}
public static long getLong(String key, long defaultValue) {
try {
return Long.parseLong(prop.getProperty(key, String.valueOf(defaultValue)));
} catch (Exception e) {
return defaultValue;
}
}
}
读取properties文件的更多相关文章
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- 用eclipse做项目中常遇到的问题-如何创建并读取properties文件
在用eclipse做项目开发的时候我们常常会将一些重要的内容写在配置文件里面, 特别是连接数据库的url,username,password等信息,我们常常会新建一个properties文件将所有信息 ...
- jsp读取properties文件
jsp读取properties文件 jsp中读取properties文件,并把值设到js变量中: mpi.properties文件内容: MerchantID=00000820 CustomerEMa ...
- java读取.properties文件
在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...
- Java 读取Properties文件时应注意的路径问题
1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题: InputStream in = this.getClass().getRe ...
- Java的Properties类和读取.properties文件
一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...
- Java-马士兵设计模式学习笔记-观察者模式-读取properties文件改成单例模式
一.概述 1.目标:读取properties文件改成单例模式 二.代码 1.Test.java class WakenUpEvent{ private long time; private Strin ...
- Java读取Properties文件的六种方法
使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...
- Android studio 读取properties文件
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath( ...
随机推荐
- maven 简单实用教程
1. Maven介绍 1.1. 简介 java编写的用于构建系统的自动化工具. 目前版本是2.0.9,注意maven2和maven1有很大区别,阅读第三方文档时需要区分版本. 1.2. Maven资源 ...
- VMWare12 安装 OSX 10.10
推荐电脑配置 1:Inter I5及以上 (A卡请自行百度大神解决方案) 必须开启CPU虚拟化:开机进入 BIOS ---> Intel Virtualization Technology--- ...
- Axis2 WebService(基于REST风格)
http://www.lifeba.org/arch/java_axis2_webservice_rest.html Axis2除了提供传统的webservice方法外,还提供了对Rest的支持.Ax ...
- CentOS+Nginx一步一步开始配置负载均衡
Nginx负载均衡的理解 http://www.linuxdiyf.com/linux/10205.html Nginx是一个轻量级的.高性能的WebServer,他主要可以干下面两件事: 作为htt ...
- 【CSS3】Advanced7:CSS Transitions
1.animate parts of your design without the need for the likes of JavaScrip 2.allowing smooth animati ...
- synchronize学习
这个例子我们看到,java中将对象或者Class对象当做锁 package synchronized简单使用; public class Test7 extends Thread{ public st ...
- bzoj 3172 [Tjoi2013]单词(fail树,DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3172 [题意] 题目的意思是这样的,给若干个单词,求每个单词在这一堆单词中的出现次数. ...
- HDOJ-ACM1097(JAVA) A hard puzzle
这道题就是HDOJ的1061的变形: 1061 :求n的n次方的个位数 http://www.cnblogs.com/xiezie/p/5596779.html 1097 :求n的m次方的个位数 因此 ...
- Kooboo 加Search功能 必须先ReBuild Index Data
加Search功能 有几个要点 1. 需要在Kooboo 必须先 ReBuild Index Data 2. 需要在要搜索的page中启用搜索索引 搜索的代码 @using K ...
- Step-by-Step Guide to Portal Development for Microsoft Dynamics CRM - 摘自网络
The Challenge Oftentimes in the world of Dynamics CRM, the need arises for non-CRM users to gain acc ...