如何读取properties中的数据

xiaolihui333 2003-11-11 09:27:34
为什莫不能读properties中数据
...全文
595 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaprogramlover 2003-12-11
  • 打赏
  • 举报
回复
学习中!
wellsoon 2003-11-11
  • 打赏
  • 举报
回复
1.先写jsp代码如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*"%>
<html>
<head>
<title>
引用资源文件的演示
</title>
</head>
<body bgcolor="#ffffff">
<h1>
引用资源文件的演示例子
</h1>
<%
String sDriver="";
String sUser="";
String sPasswd="";
String sDsn="";
try
{
ResourceBundle res;
res = ResourceBundle.getBundle("testres");
sDriver = res.getString("DRIVER");
sUser = res.getString("USERID");
sPasswd = res.getString("PASSWORD");
sDsn= res.getString("DSN");
}
catch(Exception e){out.println("Error:ResFileNotFound!");return;}
out.println("DRIVER为:"+sDriver);
out.println("<br>USERID为"+sUser);
out.println("<br>PASSWORD为"+sPasswd);
out.println("<br>DSN为:"+sDsn);
%>
</body>
</html>

2.建立资源文件
主菜单->File->New File->Type选择properties,文件名testres,存放路径随便选择。
点击OK即可。

3.编辑资源文件.
写如以下内容:
DRIVER=COM.ibm.db2.jdbc.net.DB2Driver
DSN=jdbc:db2://192.168.11.3:38868/mser
USERID=db2inst1
PASSWORD=11c1mari
保存。

4.在工程里添加资源文件。
主菜单->Project->Add Files/Packages->选择刚才创建的testres.properties。
点击OK即可。

5.运行jsp,此时页面输出为:
引用资源文件的演示例子
DRIVER为:COM.ibm.db2.jdbc.net.DB2Driver
USERID为db2inst1
PASSWORD为11c1mari
DSN为:jdbc:db2://192.168.11.3:38868/mser

资源文件添加成功了!!!



2003-9-8 12:44:00
修改笔记 发表评语???

2003-9-8 19:12:42 关于存储.properties文件的操作参考连接:
http://www.javaresearch.org/article/showarticle.jsp?column=1&thread=4289

package beanservlettest;

import java.util.*;
import java.io.*;

public class poptest {
public poptest() {
}

public static void main(String[] args) {
poptest poptest1 = new poptest();
try{ poptest1.saveProperties();}
catch(Exception e){
System.out.println("出错了:" + e.getMessage());
}
}

private void saveProperties() throws Exception {
Properties prop = new Properties();
FileOutputStream fos;
FileInputStream fis;
try {
fis = new FileInputStream(new File("c:\\config.properties"));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
throw new Exception("File config.properties NOT found!");
}
try {
prop.load(fis);
}
catch (IOException ex1) {
ex1.printStackTrace();
throw new Exception("Error while read from config.properties!");
}
System.out.println("other is:" + prop.getProperty("other"));
prop.setProperty("setting1", "value2");
System.out.println("other is:" + prop.getProperty("other"));
try {
fos = new FileOutputStream(new File("c:\\config.properties"));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
throw new Exception("File config.properties NOT found!");
} //save the properties
try {
prop.store(fos, null);
}
catch (IOException e) {
e.printStackTrace();
throw new Exception("Error when save the config.properties.");
}

}
}


81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧