读取properties文件

package com.hm.common.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
final static String fileName = "/conf/common.properties";

// 获取key所对应的值
public static String getProperties(String key) {
Properties prop = new Properties();
InputStream is = null;
try {
// properties文件放在src目录的下边
is = PropertiesUtil.class.getResourceAsStream(fileName);
if (is == null)
is = new FileInputStream(fileName);
prop.load(is);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return prop.getProperty(key);
}

public static void main(String[] args) {
System.out.println( PropertiesUtil.getProperties("a.vn.salt"));
}

}

package com.la.util;

import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.logging.Logger;

public class configResponse {

// protected static Logger logger = Logger.getLogger(configResponse.class);
protected static ResourceBundle configResponse;

protected static final String config_response_file="config/configResponse.properties";

static {

try {
configResponse = PropertyResourceBundle.getBundle(config_response_file);

} catch (Exception e) {
// TODO: handle exception
}
}

public static int getIntegerCode(String key) {
try {
if (configResponse.getString(key) != null) {
return Integer.parseInt(configResponse.getString(key));
}
} catch (Exception e) {
// logger.error("RESPONSE CODE FROM STRING TO INT THEN ERROR." , e) ;
}
return 0 ;
}
/**
* update by Brain
* @param key
* @return
*/
public static String getStringCode(String key){
return configResponse.getString(key)!=null?configResponse.getString(key):"";
}

public static int getIntegerCode(String key , int value ) {
try {
if (configResponse.getString(key) != null) {
return Integer.parseInt(configResponse.getString(key));
}
} catch (Exception e) {
// logger.error("RESPONSE CODE FROM STRING TO INT THEN ERROR." , e) ;
}
return value ;
}

public static String get(String key) {
return configResponse.getString(key);
}

public static String get(String key , String value) {
return configResponse.getString(key)==null?value:configResponse.getString(key) ;
}

}

时间: 2024-08-28 17:29:21

读取properties文件的相关文章

五种方式让你在java中读取properties文件内容不再是难题

一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC+Mybatis整合开发的项目中通过java程序读取properties文件内容的方式进行了梳理和分析,先和大家共享. 二.项目环境介绍 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Id

在JSP页面中读取properties文件

在做web开发时,经常遇到要修改一下配置信息.如果把这些配置信息写在代码中,后期的维护便会比较麻烦.所以,一般都是把配置信息写在配置文件里面. 在JSP文件中,如果想要调用properties文件中的变量,则要在有文件中引入 java.util.ResourceBundle 类: <%@ page contentType="text/html; charset=UTF-8" import="java.util.ResourceBundle" %> 已知配

Java读取Properties文件的六种方法

使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2.使用java.util.ResourceBundle类的getBundle()方法 示例: ResourceBundle rb

转载:java基础学习总结——java读取properties文件总结

java基础学习总结--java读取properties文件总结 一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的

Java读取.properties文件

例1: 创建一个config文件夹 config文件夹中有一个Properties.properties文件 内容为: capitalLetter=ABCDE smallLetter=abcde 注意:config文件夹与包含Test类的包为同一级 import java.io.IOException; import java.util.Properties; public class Test { public static void main(String[] args) { Propert

java读取.properties文件及解决中文乱码问题

Java项目中有些信息(例如web的配置信息)可能要放在.properties文件中,那我们应该怎么来读取它们呢,下面给出一个工具类做一说明,并解决了中文乱码问题: 1.其中config.properties文件信息如下: name=\u843D\u82B1\u6709\u610Fwang王 str=\u6D41\u6C34\u65E0\u60C5 boolean=true 2.PropertiesUtil工具类读取.properties文件 import java.io.BufferedInp

用java读取properties文件--转

今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest public static void readFile(String fileName) {//传入参数fileName是要读取的资源文件的文件名如(file.properties) InputStream in = null; Properties pros = new Properties(); tr

Java实现动态加载读取properties文件

问题: 当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties prop = new Properties(); prop.load(classLoader.getResourceAsStream("/Application.properties")); 会发现修改了.properties后,即使重新执行,读入的仍为修改前的参数.此问题的原因在于Cla

读取Properties文件以及中文乱码问题

在java类中常见的读取Properties文件方式,是使用Properties.load(inputStream);的方式但是常常出现中文乱码问题,这就很尴尬了 public synchronized void load(InputStream inStream) throws IOException { load0(new LineReader(inStream)); } 看了很久才发现,还有一个重载的方法, 它的参数是Reader,如下: public synchronized void

java基础学习总结——java读取properties文件总结

一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的目录结构 1.2. java读取properties文件代码测试