应用程序配置文件app.properties中包含如下一些信息:
name=administor
pwd=554499
port=8000
怎么获取这个文件中的值呢?(最好使用Singleton的方式,有其它方式也行)
哪位兄台知道的话,麻烦进来指点指点!!!

解决方案 »

  1.   


        Properties props = new Properties();
        InputStream in = new FileInputStream(new File("app.properties"));
        props.load(in);
        in.close();
        System.out.println("name = "+props.get("name"));
      

  2.   

    原来是这么写的,谢了! 不过我把app.properties放在src目录下,怎么读取的时候老是报“系统找不到指定的路径”
      

  3.   

    Properties props = new Properties();
        InputStream in = new FileInputStream(new File("app.properties"));
        props.load(in);
        in.close();
        System.out.println("name = "+props.get("name"));
      

  4.   

        public final static String APP_CONFIG_FILENAME = "app.properties";
        
        public final static String NAME= "name";
        
        public final static String PWD = "pwd";
        
        public final static String PORT = "port";
           
        public static void init()
        {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream(APP_CONFIG_FILENAME);
            Properties properties = new Properties();
            try
            {
                properties.load(inputStream);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            String name= properties.getProperty(NAME);
            String pwd= properties.getProperty(PWD);
            String port= properties.getProperty(PORT);
              }
      

  5.   

    相对路径的写法: InputStream in = new FileInputStream(new File("../../app.properties"));../代表此类的上一级目录。