Properties properties=new Properties();
InputStream is=null;
try{
   is=getClass().getResourceAsStream(FILE);
   properties.load(is);
}catch(Exception e){
   e.printStackTrace();
}finally{
   try{
     if(is!=null) is.close();
    }catch(IOException e){
      e.printStackTrace();  
    }
}
String port=properties.getProperty("PORT");

解决方案 »

  1.   

    ZeroC,谢谢你!非常感谢!可是,Properties类是你自己的类吗?我找不着,编译不了!
      

  2.   

    java.lang.Object
      |
      +--java.util.Dictionary
            |
            +--java.util.Hashtable
                  |
                  +--java.util.Properties
      

  3.   

    import java.util.*; 就可以了:)
      

  4.   

    in=getClass().getResourceAsStream("d:\\java\\Test52\\Test52.ini");
        if(in==null){
          System.out.println("in is null");
          return;
        }
        try{
          properties.load(in);
          System.out.println(properties.getProperty("Port"));
        }
        catch(IOException e){
          System.out.println(e.getMessage());
        }那个文件存在,但是怎么in总是null呀,这是为什么呀?
      

  5.   

    我也是,in一直为null,为什么呢?
      

  6.   

    将*.ini文件名改为*.properties,试试看。
      

  7.   

    我的目的就是为了读取*.ini里面的数据,把*.ini改为*.properties,那不违背了问题的本意了吗。
      

  8.   

    给你我的一个读文件的方法原码,你看看有没有用。private  boolean readConfigFile(String fileName){
                Properties prop = new Properties();
                FileInputStream fis = null;
                int port = 0;
      
       boolean inflag = false;
                   
       try {
                       fis = new FileInputStream(new File("." + File.separator + fileName));
                       prop.load(fis);
                       port = Integer.parseInt(prop.getProperty("PORT"));
                       fis.close();
                       inflag = true;
                 }
                 catch (IOException ex1) {
                     port = 8888;
                     inflag = false  ;
                 }
                 return inflag;
    }// end of readConfigFile
      

  9.   

    文件内容格式
    #[SectionName]
    PropName1=PropValue1
      

  10.   

    jh20001(十七郎),非常感谢,能用,但有点不明白,请问fis = new FileInputStream(new File("." + File.separator + fileName))里面File的参数"." + File.separator + fileName是怎么回事?为什么要这样?还有,结果是出来了,可是为什么要这样却还是不明白,读取普通的*.txt文件特别简单,为什么读取*.ini却这么麻烦呢?还请多多指教,谢谢,万分感谢!!!
      

  11.   

    另外,还有一个问题,就是prop.getProperty("PORT","8888")方法使用默认值的问题,为什么只有在程序找不到*.ini文件的时候才能返回默认值8888?为什么当程序能找到*.ini文件,但文件里面没有PORT数据时,方法不返回默认值8888呢?
      

  12.   

    :)!谢到不必了,大家互相学习吗。"." + File.separator + fileName,其中“.”中指出当前的路径,File.separator 好象是将所要读的文件的后缀默认为Properties能读的文件属性,也就是将*.ini默认为*.properties,你所说的第2个问题我到没试过,你是在文件里没有PORT项,还是“PORT=”?
      

  13.   

    我找到第二个问题的原因了,             catch (IOException ex1) {
                     port = 8888;
                     inflag = false  ;
                 }
                 
    我只捕获了IOException错误,将IOException改为Exception就可以了,你试试看!
      

  14.   

    to jh20001(十七郎) :      假设配置文件为:sheng.ini    假设文件内容为:
    [SETINGS]
    IP=192.168.0.1
    PORT=5555我的意思是,getProperty()方法有两种,一种是带一个参数的getProperty(String name),另一种是带两个参数的getProperty(String name,String defaultValue),用的是第二个方法,如果能找到文件中的name,则返回name后面的值(假如配置文件如上,假设name为PORT)。问题就处在这:如果找不到sheng.ini文件,则返回defaultValue,如果能找到sheng.ini,但找不到文件内的PORT,则什么都不返回,我需要的是,能否让他在能找到sheng.ini但找不到PORT的情况下也返回defaultValue???盼回复!!!!!!