InputStream is = getClass().getResourceAsStream("/mmq.ini");
Properties dbProps = new Properties();
try {
dbProps.load(is);
}
catch (Exception e) {
writeMsg("不能读取属性文件. " +
"请确保mmq.ini在CLASSPATH指定的路径中");
return;
}
String item1 = dbProps.getProperty("item1");
大体就这样读的。你改改吧。
Properties dbProps = new Properties();
try {
dbProps.load(is);
}
catch (Exception e) {
writeMsg("不能读取属性文件. " +
"请确保mmq.ini在CLASSPATH指定的路径中");
return;
}
String item1 = dbProps.getProperty("item1");
大体就这样读的。你改改吧。
学习一下Properties类
将你的文件命名为test.properties 同一目录下面试试看看
import java.util.*;
public class resBundle{
public static void main(String args[])
try{
ResourceBundle resbndle= ResourceBundle.getBundle("test");String name=(String)resbndle.getString("item1");System.out.print(name);
}
catch(Exception e)
{
e.printStackTrace();
}
}
to : gyscsdn(geng) 你说的怎么加?我是新手。
to farmer0512(风总是朝我吹) ,不知道如何做?更无从下手
//一定要 import java.util.*;static void GetKeyNames(String fileName) {
try {
PropertyResourceBundle configBundle =
(PropertyResourceBundle) PropertyResourceBundle.getBundle(fileName);
Enumeration enum = configBundle.getKeys();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
String value = configBundle.getString(name);
} } catch (Exception e) {
e.printStackTrace();
} }配置文件的格式如下:
test.properties(文件名改为.properties)#这是注释掉的行
username = "sa"
password = "123455"
FileInputStream objDB = new FileInputStream("plmm.ini");
Properties objPro = new Properties();
objPro.load(objDB);
poolName = objPro.getProperty("poolName");
user = objPro.getProperty("user");
password = objPro.getProperty("passwd");
objDB.close();
objDB = null;
} catch(Exception e) {
//System.err.println(e.toString());
}
import java.io.*;
如果你将所有的配置信息读出到一个HashTable中再取值来用,那用我的方式就方便多了。
如果你只是想取得两个值,则用 amortal(7086) 的可能更方便了。随你喜欢呵呵。
方法还是很多的呵呵。
//获得一个配置文件中的参数
//一定要 import java.util.*; try {
PropertyResourceBundle configBundle =
(PropertyResourceBundle) PropertyResourceBundle.getBundle("test.properties");
String userName = configBundle.getString("username"); //取得配置文件中的 username的值
String password = configBundle.getString("password"); //取得配置文件中的 password的值
} } catch (Exception e) {
e.printStackTrace();
}
配置文件的格式如下:
test.properties(文件名改为.properties)#这是注释掉的行
username = "sa"
password = "123455"
FileInputStream objDB = new FileInputStream("plmm.ini");
Properties objPro = new Properties();
objPro.load(objDB);
poolName = objPro.getProperty("poolName");
user = objPro.getProperty("user");
password = objPro.getProperty("passwd");
objDB.close();
objDB = null;
} catch(Exception e) {
//System.err.println(e.toString());
}
用你的文件的绝对路径替换掉“plmm.ini”不可以吗?郁闷ing
是啊,那个plmm.ini文件只是示范给你看的。你完全可以替换成 c:\webroot\plmm.ini 这样的绝对路径呀! 不过现在微软自己都提倡不用ini文件来做为程序的配置文件,而应该换为xml文件了。
不过也没什么,因为微软的windows2003里还不是照样有数不清的ini文件。
我觉得你将扩展名改为 .properties 可能更规范一点吧。
我可以向你保证,前面发给你的两个例子都是完全可以正常运行的。
import java.io.*;
public class ResBundle
{
public static ResourceBundle mess=null;
public static void main(String[] args)
{
try
{
getBoundle();
String msg=getMessage("item1");
System.out.println(msg);
String msg2=getMessage("item2");
System.out.println(msg2); }catch(Exception e){e.printStackTrace();}
}
public static ResourceBundle getBoundle()
{
try
{
return ResourceBundle.getBundle("ResBundle");
}
catch(MissingResourceException _ex)
{
System.out.println("Resource file Messages not found");
return null;
}
}
public static String getMessage(String key)
{
String ret = null;
try
{
ret = mess.getString(key);
}
catch(MissingResourceException _ex)
{
ret = null;
}
return ret; }
}这个比较好调用
修改一下内容为: 保存在文件ResBundle.properties 里面 我放到jdk\bin下面执行没有问题
#[多媒体主菜单]
item1=欢迎您
item2=业务范围
item3=章程
item4=业务种类一览表#[业务范围]
yw1=整存整取
yw2=零存整取
yw3=活期储蓄
yw4=代发工资
yw5=存本取息
你的相对路径确定以后,你的plmm.ini文件的绝对路径不也就有了吗?
就像楼上的说的那样自己处理一下就ok了!
现在的妹妹啊~,继续郁闷ing
import java.util.ResourceBundle;
import java.io.*;
public class ResBundle
{
//public static String msg[]=null;
private static ResourceBundle mess=null;
public static void main(String[] args)
{
mess=getBoundle();
String msg[]={getMessage("item1"),getMessage("item2"),getMessage("item3"),getMessage("item4")};
for(int i=0;i<msg.length;i++)
System.out.println(msg[i]);
}
public static ResourceBundle getBoundle()
{
try
{
return ResourceBundle.getBundle("ResBundle");
}
catch(Exception _ex)
{
System.out.println("Resource file Messages not found");
return null;
}
}
public static String getMessage(String key)
{
String ret = null;
try
{
ret = mess.getString(key);
}
catch(Exception _ex)
{
ret = null;
}
return ret;
}
}
非常感谢你的关注和支持,但是我不知道是我理解能力问题,还是其他的问题,我现在还是不能调试成功,我用的是JBuilder7.0,放在jdk\bin,为什么需要那么做?而且假如我打包后,我觉得有很多问题的,不过我会继续调试我试了很多方法还是没有解决,比较烦琐。已经很没信心了。因为如果mmq.ini中增加和删除item一些项后,这样的读数据灵活语句又该怎么写,而现在我做了一个用数据库来增加,修改,删除的管理界面。这样实现比较方便。但是做了这个想法,读写ini,确实是一个不错的方法,我会继续关注和编写,希望各位多多关照,也可以另开帖子。
native2ascii mmq.properties mmq_zh_CN.properties
(2)要jsp调用,jsp页面增加
<%@page import="java.util.*"%>
然后
<%
ResourceBundle prop = ResourceBundle.getBundle("application");
Enumeration enum = prop.getKeys();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
String value = prop.getString(name);
System.out.println(value);
}
%>
(3)再修改下,应该没有什么问题了。。
但是我有两个问题(接楼主mm的问题)
1。如果配置文件中在不同的section中有相同的两个key name,如何处理?我实验过,读出的总是最后一次出现的那个key name的值:比如abc.ini
[server]
ipaddr=10.1.10.10
[agent]
ipaddr=10.1.10.11
[client]
name=beijing
那么用getprotperty读出的就是10。1。10。11,而不是10。我如何才能多到上面的那个那个10的地址值呢?2。如果想修改上面ini文件中server中的ip地址,该如何进行呢?好象property类没有相应的方法?(声明:我是菜鸟)