java中如何用输入输出流读取属性文件。
解决方案 »
- 想学习ssh2+buffalo
- 问个struts2验证出错返回保存原始填写数据问题
- 关于JSF自定义组件重复生成
- 除endorsed方法外还有办法能让jax-ws2.1覆盖jax-ws2.0吗?
- iBatis与Hibernate那个所支持的同时在线访问量更大?
- struts2界面问题
- jdbc连接不上oralce数据库,请各位帮忙!
- 谁有weblogic 6.1 的 ejb20.jar ?发到我信箱([email protected] or [email protected])可得100大元。在线等待
- 做了一个基于J2EE的邮件服务器
- 迷惑:CMP 实体Bean 跟数据库的关系 ... ...
- 想交几位"尚学堂"毕业的学生请教和探讨IT学术问题……
- 定义的Filter有顺序吗
xxxx.properties public static String getProConfig(String res) {
ResourceBundle rs = null;
String s = "";
try {
rs = ResourceBundle.getBundle("xxxx");
s = rs.getString(res);
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.MissingResourceException;
import java.util.ResourceBundle;public class publishLicenceKey { private static publishLicenceKey instance = new publishLicenceKey();
private static ResourceBundle bundle;
public publishLicenceKey(){
bundle = ResourceBundle.getBundle("SqlConn");
} public static publishLicenceKey getInstance() {
return instance;
} public static void main(String[] args) throws Exception{ String url = "jdbc:postgresql://"+getValue("PostgreSeverIP")+"/"+getValue("DatabaseName");
Class.forName("org.postgresql.Driver");
String user = getValue("UserName");
String pwd = getValue("Password");
}
private static String getValue(String keyWord) {
try {
return bundle.getString(keyWord);
} catch (MissingResourceException mre) {
return null;
}
}
}属性文件
PostgreSeverIP = 127.0.0.1
DatabaseName = key
UserName = postgres
Password = admin getValue("PostgreSeverIP") 就是例子,调用getValue()就可以
bundle = ResourceBundle.getBundle("SqlConn");
}里面的SqlConn就是属性文件
SqlConn.properties
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;//单例模式实现读取zxc.properties文件的内容
public class OVLoadProperties {
//声明一个自己的实例
private static OVLoadProperties instance = new OVLoadProperties();
//返回该实例
public static synchronized OVLoadProperties getInstance() {
return instance;
}
//获取key所对应的值
public static String getProperties(String key) {
Properties p = new Properties();
String fileName = "zxc.properties";
InputStream is = null;
try {
//zxc.properties文件放在src目录的下边
is = OVLoadProperties.class.getResourceAsStream(fileName);
if (is == null)
is = new FileInputStream("fileName");
p.load(is);
} catch (Exception e) {
System.out.println("加载文件出错啦!" + e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
return p.getProperty(key);
} public static void main(String args[]) {
System.out.println(OVLoadProperties.getProperties("zxc"));
}
}
zxc.properties文件的内容为:zxc = 1234
输出结果:1234
ResourceBundle 这个类是用来读取国际化资源文件吧,读取一般的配置文件一般借助Properties的这个类
这里的db.properties是存在了src下
private static final String CONFIG_FILE = "system.config";
private static Properties config = new Properties();
private static long confmodified = 0;
static Logger log =
Logger.getLogger(Config.class.getName()); private static boolean loadConfig() {
/**
* ��ȡ�����ļ�
*/
ClassLoader classLoader =
Config.class.getClassLoader();
File configfile = null;
URL url = null;
if(classLoader!=null){
url = classLoader.getResource(CONFIG_FILE);
configfile = new File(url.getFile());
}
try {
long l = configfile.lastModified();
if (l > confmodified) {
config.clear();
config.load(new FileInputStream(configfile));
confmodified = l;
}
return true;
} catch (IOException e) {
return false;
}
}
public static Enumeration configNames(){
loadConfig();
return config.propertyNames();
}
public static String getConfig(String key, String def) {
synchronized (config) {
if (!loadConfig()){
return def;
}
String s = config.getProperty(key, def);
return s;
}
} public static int getConfig(String key, int def) {
int r = def;
try {
String s = getConfig(key, "");
if (s != null) {
r = Integer.parseInt(s);
}
} catch (NumberFormatException e) {
}
return r;
}
}
参考一下,可以
我的配置文件中只有两行,column=50
id=10
以下是读取配置文件的代码示例。import java.util.Properties; // 读取属性配置文件的工具类
import org.apache.commons.io.IOUtils; // IO工具类,其中安静的释放对象很好用public void propertisReader()
{
Properties propt = null;
InputStream is = null;
try
{
is = new FileInputStream("c:/config.properties"); // 根据文件的路径创建输入流
propt = new Properties();
propt.load(is); // 加载配置文件到propt对象中
BigDecimal column = new BigDecimal(propt.getProperty("column ")); // 一旦配置文件加载到propt对象中后可以使用getProperty("base"))的方法取得属性值。
BigDecimal id = new BigDecimal(propt.getProperty("id"));
}
catch (Exception e)
{
throw new IllegalArgumentException("加载配置文件失败。",e);
}
finally
{
IOUtils.closeQuietly(is); // 安静的释放资源
}
}
import java.io.File;
import java.io.FileReader;/**
* 提供对文件的一些常用操作 3、设计程序Combine.java,将d:\java文件夹下的所有扩展名为txt的文件合并为一个文件,命名为cc.txt
*
* @author Rain
*
*/
public class Combine {
/**
* 将文件的内容读取到一个String字符串中并返回
*
* @param Path
* String文件路径
* @return 文件的内容
*/
public static void getTxtContent(String path) {
File f = new File(path);
String txtName[] = f.list();
int i;
for (i = 0; i < txtName.length; i++) {
File file = new File(path + txtName[i]);
if (file.isFile()
&& txtName[i].substring(txtName[i].length() - 4).equals(
".txt")) {
FileReader reader = null;
BufferedReader buf = null;
StringBuffer content = new StringBuffer();
try {
reader = new FileReader(path);// 需处理异常
buf = new BufferedReader(reader);// 建立BufferedReader对象,并实例化为br,不需处理异常
String line = buf.readLine();// 从文件读取一行字符串
while (line != null) {
content.append(line + "\n");
line = buf.readLine();// 从文件中继续读取一行数据
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
buf.close();
} catch (Exception ex2) {
ex2.printStackTrace();
}
try {
reader.close();
} catch (Exception ex2) {
ex2.printStackTrace();
}
}
} }
}}