当前位置: 技术问答>java相关
请教用java从properties文件中读取属性字段的方法?
来源: 互联网 发布时间:2015-01-14
本文导语: 我编写一个连接数据库的bean时,需要从一个properties文件中读取DriverName, UserName等字段,请问各位大侠用java实现的具体方法? | Properties p = new Properties(); p.load(new FileInputStream("*.properties")); ...
我编写一个连接数据库的bean时,需要从一个properties文件中读取DriverName, UserName等字段,请问各位大侠用java实现的具体方法?
|
Properties p = new Properties();
p.load(new FileInputStream("*.properties"));
String DriverName = (String) p.get("DriverName");
String UserName = (String) p.get("UserName");
......
p.load(new FileInputStream("*.properties"));
String DriverName = (String) p.get("DriverName");
String UserName = (String) p.get("UserName");
......
|
看看ResourceBundel类
及
它的子类PropertyResourceBundle类.
及
它的子类PropertyResourceBundle类.
|
import java.io.*;
import java.util.*;
public class EnvPro
{
public static Properties getEnv(String filename)
{
Properties p = new Properties();
try
{
InputStream is = new FileInputStream(filename);
p.load(is);
}catch(Exception e)
{
System.out.println(e.toString());
}
return p;
}
}
import java.util.*;
public class EnvPro
{
public static Properties getEnv(String filename)
{
Properties p = new Properties();
try
{
InputStream is = new FileInputStream(filename);
p.load(is);
}catch(Exception e)
{
System.out.println(e.toString());
}
return p;
}
}
|
使用ResourceBundle就可以了
try{
ResourceBundle d=ResourceBundle.getBundle(filename);
String s=d.getString(keyname);
}
catch
....
try{
ResourceBundle d=ResourceBundle.getBundle(filename);
String s=d.getString(keyname);
}
catch
....