java读取配置文件properties
Ⅰ java怎么读取properties文件
/*** 用来测试获取配置文件的类* @author BaiKeyang**/public class ResourceUtils {public static String getValue(String file, String key) { String value; // 未取到值,从properites文件中查找 try { ResourceBundle bundle = ResourceBundle.getBundle(file); value = bundle.getString(key); if (value==null){ throw new RuntimeException("没有该属性的值:"+key); } value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); return value; } catch (Throwable e) { System.err.println(e); return null; } }public static void main(String[] args) {String resourceFile = "config.dataBase";// 创建一个默认的ResourceBundle对象 // ResourceBundle会查找包config下的dataBase.properties的文件 // config是资源的包名,它跟普通java类的命名规则完全一样: // - 区分大小写 // - 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样 // - 资源文件必须位于指定包的路径之下(位于所指定的classpath中) // 假如你是在非Web项目中使用,则一定要写资源文件的路径,也就是包路径必须存在。// 如果是Web项目,不写包路径可以,此时将资源文件放在WEB-INF\classes\目录下就可以。String url = getValue(resourceFile, "db.url");System.out.println(url);}}
Ⅱ java怎么调用properties配置文件的值
projectPath=11
aa= 11
public class ConfigTool {
static String projectPath = "";
public static HashMap<String,String> hashMap = new HashMap();
static {
Properties property = new Properties();
InputStream in = ConfigTool.class.getResourceAsStream("/config.properties");
try {
property.load(in);
projectPath = property.getProperty("projectPath").trim();
hashMap.put("huifu", property.getProperty("aa"));
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (null != in) {
try {
in.close();
} catch (Exception e) {}
}
}
}
}
Ⅲ java程序读取properties配置文件出现中文乱码
这个问题有两种办法:
第一种办法:如楼上所说的那样也可以,就是native2ascii -reverse -encoding gb2312 user.properties ActionName_zh_CN.properties这样以后,你打开ActionName_zh_CN.properties的内容,再将ActionName_zh_CN.properties文件这样置:native2ascii ActionName_zh_CN.properties userChange.properties 然后你的程序读userChange.properties的内容就可以。
第二种办法是:
如你代码里写的,你可以在String username=property.getProperty("username");之后,添加JAVA代码页可以将乱码转为中文的。用如下语句就可以了,resultName=new String(username.getBytes("ISO-8859-1"),"gbk"); 然后再用resultName就可以了,不过这样的话你下面的String password=property.getProperty("password");
都慢慢的通过上面的java代码去转。
不知道你对反射熟悉不?如果熟悉的话可以通过反射机制去做第二种办法的转码就方便多了!
Ⅳ 在java中如何读取properties文件
使用java.util.Properties
1、创建一个Properties对象。
2、使用对象的load方法加载你的property文件。
3、使用getProperty方法取值。
例子:
package com.bill.test;
import java.io.FileInputStream;
import java.util.Properties;
public class Test {
public static void main(String[] args) throws Exception{
Properties property = new Properties();
property.load(new FileInputStream("你的文件位置"));
String value = property.getProperty("你的属性的key");
//TODO 使用value...
}
}
Ⅳ java中怎么读取properties文件
(1)load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到内Properties类对象。如下面容的代码:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();
(2)store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();
Ⅵ 求用java读写properties文件的代码
Java可使用Properties类读写properties,具体说明如下:
1.Properties类与Properties配置文件
Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。
2.Properties中的主要方法
(1)load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();
(2)store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();
如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空则没有注释信息。
注释信息后面是属性文件的当前保存时间信息。
(3)getProperty/setProperty
这两个方法是分别是获取和设置属性信息。
3.代码实例
属性文件a.properties如下:
name=root
pass=liu
key=value
读取a.properties属性列表,与生成属性文件b.properties。代码如下:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
try {
// 读取属性文件a.properties
InputStream in = new BufferedInputStream(new FileInputStream("a.properties"));
// /加载属性列表
Properties prop = new Properties();
prop.load(in);
Iterator<String> it = prop.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + ":" + prop.getProperty(key));
}
in.close();
// /保存属性到b.properties文件
FileOutputStream oFile = new FileOutputStream("b.properties", true);// true表示追加打开
prop.setProperty("phone", "10086");
prop.store(oFile, "The New properties file");
oFile.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Ⅶ java读取properties配置文件路径问题
可以直接通过Thread方法直接获取到项目路径下的配置文件:
static {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("BankConPort.properties"); //加载线程文件成为流
Properties prop = new Properties();
try {
prop.load(is);//直接转换为对象
BOB_ReqURI = prop.getProperty("BOB_ReqURI");
BOB_SignURI = prop.getProperty("BOB_SignURI");
BOBLOGINPASSWORD = prop.getProperty("BOBLOGINPASSWORD");
} catch (IOException ex) {
java.util.logging.Logger.getLogger(BOBUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.info("解析信息出错", e.getMessage());
}
}
}
}
Ⅷ java代码怎么获取properties文件的内容
importjava.util.Properties;
publicclassPropertiesUtil{
privatestaticPropertiesinit=null;
privatestaticPropertiesutil=null;
privatestaticPropertieschid=null;
(){
if(init==null){
try{
init=newProperties();
init.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("init.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returninit;
}
(){
if(util==null){
try{
util=newProperties();
util.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("util.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returnutil;
}
(){
if(chid==null){
try{
chid=newProperties();
chid.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("chid.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returnchid;
}
/**
*获取属性配置文件参数值
*@paramkey参数名称
*@paramdef参数默认值
*@return参数值
*/
publicstaticStringget(Stringkey,Stringdef){
Stringval=getInit().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}
publicstaticlonggetlong(Stringkey,longdef)
{
try{
def=Long.parseLong(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}
/**
*获取属性配置文件参数值
*@paramkey参数名称
*@paramdef参数默认值
*@return参数值
*/
publicstaticintget(Stringkey,intdef){
try{
def=Integer.parseInt(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}
publicstaticlongget(Stringkey,longdef){
try{
def=Long.parseLong(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}
/**
*获取属性配置文件参数值
*@paramkey参数名称
*@paramdef参数默认值
*@return参数值
*/
publicstaticStringgetUtil(Stringkey,Stringdef){
Stringval=getUtil().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}
publicstaticlonggetUtil(Stringkey,longdef){
longval=Long.parseLong(getUtil().getProperty(key));
if(val==0){
returndef;
}
returnval;
}
/**
*获取属性配置文件参数值
*@paramkey参数名称
*@paramdef参数默认值
*@return参数值
*/
(Stringkey,Stringdef){
Stringval=getChid().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}
importcom.jinlou.util.PropertiesUtil;
publicclassTest{
publicstaticvoidmain(String[]args){
//从配置文件中去key=test的value如果配置文件中无此key则返回默认值
Stringtest=PropertiesUtil.get("test","默认值");
System.out.println(test);
}
}
Ⅸ 如何在java类中读取Properties配置文件
最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用: InputStream ins = this.getClass().getResourceAsStream(