读取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( ...
随机推荐
- jQuery name属性与checked属性结合获取表单元素值
var paytype = $("input[name='paytype']:checked").val(); alert(paytype); input元素下名称为paytype ...
- oracle的一种字符串处理机制。
orcale会把空字符串当成Null进行存储,sqlserver直接存储空字符串
- HDU 1061
#include<stdio.h> #include<string.h> int a[10]; int main() { int T,n,i,k,temp,b,t; scanf ...
- 如何处理alert、confirm、prompt对话框
import java.io.File; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.ope ...
- nyoj VF函数
大意就是: 在1到在10的9次方中,找到各个位数和为固定值s的数的个数, 首先我们确定最高位的个数,为1到9: 以后的各位为0,到9: 运用递归的思想,n位数有n-1位数生成 f(n)(s) +=f( ...
- oracle rac IP详解
rac环境下vip/public/private IP的区别 每个节点要2块网卡, 3个IP,虚拟IP或者叫做业务IP,单个网卡当掉可以“漂”到其他网卡是继续提供服务 在Oracle RAC环境下,每 ...
- office文件密码破解方法及软件
今天会用到3个软件 1.Office Password Remover 说明:这个软件可以很快破解.doc .xls的密码 使用方法:参考百度经验里面的文章http://jingyan.baidu ...
- ZOJ-3720 Magnet Darts 计算几何,概率
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3720 题意:在一个矩形区域投掷飞镖,每个整点有磁性,每个点的磁性 ...
- POJ3280 - Cheapest Palindrome(区间DP)
题目大意 给定一个字符串,要求你通过插入和删除操作把它变为回文串,对于每个字符的插入和删除都有一个花费,问你把字符串变为回文串最少需要多少花费 题解 看懂题立马YY了个方程,敲完就交了,然后就A了,爽 ...
- Git学习资料
1.http://wuyuans.com/2012/05/github-simple-tutorial/ 2.http://www.ihref.com/read-16369.html