读取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( ...
随机推荐
- hadoop异常: 到目前为止解决的最牛逼的一个异常(java.io.IOException: Incompatible clusterIDs)
(注意: 本人用的版本为hadoop2.2.0, 旧的版本和此版本的解决方法不同) 异常为: 9 (storage id DS-2102177634-172.16.102.203-50010-1384 ...
- Hadoop MapReduce 二次排序原理及其应用
关于二次排序主要涉及到这么几个东西: 在0.20.0 以前使用的是 setPartitionerClass setOutputkeyComparatorClass setOutputValueGrou ...
- ☀【offset() / position()】
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- Deep Learning Overview
[Ref: http://en.wikipedia.org/wiki/Deep_learning] Definition: a branch of machine learning based on ...
- Servlet能读到JSessionID,读不到其它cookie问题
Servlet的Cookie值保存与获取 今天测试设置和获取Cookie遇到了一点小问题,很奇怪的问题: 把J2ee服务部署在本地 8080端口:访问任何一个服务时,如果客户端没有cookie,则下发 ...
- Using the Task Parallel Library (TPL) for Events
Using the Task Parallel Library (TPL) for Events The parallel tasks library was introduced with the ...
- linux网站配置文件.htaccess伪静态转换到IIS web.config中
linux下的php网站放到Windows服务器IIS下.htaccess文件伪静态规则转换. 此办法只适合于linux下的php网站放到Windows服务器IIS下,网站除了主页面正常以外子页面 ...
- The Donkey of Gui Zhou
Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped ...
- JDK1.5新特性(三)……Varargs
援引 Varargs - This facility eliminates the need for manually boxing up argument lists into an array w ...
- redis ltrim命令
LTRIM key start stop 相关命令 BLPOP BRPOP BRPOPLPUSH LINDEX LINSERT LLEN LPOP LPUSH LPUSHX LRANGE LREM L ...