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");
   大体就这样读的。你改改吧。

解决方案 »

  1.   

    同一楼上。
    学习一下Properties类
      

  2.   

    jsp与mmq.ini是同一个目录下的,请问这个路径该怎么写?
      

  3.   

    我知道用方法 ResourceBundle
    将你的文件命名为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();
    }
    }
      

  4.   

    把JSP文件夹的路径加入classpath中呀!
      

  5.   

    如果需要这样你可以通过web.xml来做,其不是更方便?
      

  6.   

    to  lcfgaoyong(云邪者) 又很多出错消息
    to : gyscsdn(geng)  你说的怎么加?我是新手。
    to  farmer0512(风总是朝我吹) ,不知道如何做?更无从下手
      

  7.   

    catch(Exception e)的错误,上面的杜配置文件没有执行,我调试时java.lang 返回的值为null
      

  8.   

    //这个方法根据你指定的文件名称编历下面的配置文件
    //一定要  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"
      

  9.   

    try {
    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());
    }
      

  10.   

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

  11.   


    如果你将所有的配置信息读出到一个HashTable中再取值来用,那用我的方式就方便多了。
    如果你只是想取得两个值,则用  amortal(7086)  的可能更方便了。随你喜欢呵呵。
    方法还是很多的呵呵。 
      

  12.   

    也许你想简单一点,简单修改一下就行了。
    //获得一个配置文件中的参数
    //一定要  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"
      

  13.   

    try {
    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
      

  14.   


      是啊,那个plmm.ini文件只是示范给你看的。你完全可以替换成 c:\webroot\plmm.ini  这样的绝对路径呀!  不过现在微软自己都提倡不用ini文件来做为程序的配置文件,而应该换为xml文件了。
     不过也没什么,因为微软的windows2003里还不是照样有数不清的ini文件。
      我觉得你将扩展名改为 .properties 可能更规范一点吧。
     
     我可以向你保证,前面发给你的两个例子都是完全可以正常运行的。
      

  15.   

    路径:config.getServletContext().getRealPath( "/" ) + "XXX.ini"
      

  16.   

    import java.util.*;
    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=存本取息
      

  17.   

    我说妹妹啊:
    你的相对路径确定以后,你的plmm.ini文件的绝对路径不也就有了吗?
    就像楼上的说的那样自己处理一下就ok了!
    现在的妹妹啊~,继续郁闷ing
      

  18.   

    属性文件名称 ResBundle.properties  放在jdk\bin下试试 我在jbuilder8下没问题
    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;
      }
    }
      

  19.   

    非常感谢各位的关心,INI文件的读取f:/mmq.ini在单单调试.jsp目录下是正常的,但是当通过java的指向后,又读不到了。to  lcfgaoyong(云邪者)
    非常感谢你的关注和支持,但是我不知道是我理解能力问题,还是其他的问题,我现在还是不能调试成功,我用的是JBuilder7.0,放在jdk\bin,为什么需要那么做?而且假如我打包后,我觉得有很多问题的,不过我会继续调试我试了很多方法还是没有解决,比较烦琐。已经很没信心了。因为如果mmq.ini中增加和删除item一些项后,这样的读数据灵活语句又该怎么写,而现在我做了一个用数据库来增加,修改,删除的管理界面。这样实现比较方便。但是做了这个想法,读写ini,确实是一个不错的方法,我会继续关注和编写,希望各位多多关照,也可以另开帖子。
      

  20.   

    (1)用properties 文件来读取,把mmq.ini改成mmq.properties ,然后把这个文件烤到/WEB-INF/CLASSES/目录下,建立一个BAT文件,里面内容为
    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)再修改下,应该没有什么问题了。。
      

  21.   

    各位的高见晚辈都看了,受益匪浅;
    但是我有两个问题(接楼主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类没有相应的方法?(声明:我是菜鸟)