zoukankan      html  css  js  c++  java
  • 类装载器读取properties资源文件

    类装载器

    public class userDAO {
        
        private static Properties dbconfig =new Properties();
        
        static{
            try {
                  InputStream instream =userDAO.class.getClassLoader().getResourceAsStream("db.propertites");
                  dbconfig.load(instream);
            } catch (Exception e) {
                throw new ExceptionInInitializerError(e);
            }
        }
    
        public void update() throws IOException {        
          System.out.println(dbconfig.getProperty("url"));        
        }
        
        public void find() throws IOException {        
                 
            }
        
        public void delete() throws IOException {        
                
            }
    
    }
    

      

    使用类装载器读取文件的弊端:

    1.文件不能太大,因为它是以装载类的方式一次性加入内存中

    2.类装载器只会加载一次,就是说不能显示文件的更新操作

    如果需要读到实时数据,就不能通过类装载器来读文件,需要通过普通的文件路径的方式

    public class DAO {
        
        public void update() throws IOException{
          
            String path = DAO.class.getClassLoader().getResource("db.properties").getPath();
            FileInputStream in= new FileInputStream(path);
            
            Properties pros = new Properties();
            pros.load(in);
            
            String url = pros.getProperty("url");    
    
        }
    
    }
    

      

    参考资料:

    http://www.cnblogs.com/tech-bird/p/3843832.html

  • 相关阅读:
    关于可控进程数的PHP多进程以及信号控制
    编译安装PHP遇到的问题
    MySQL数据的导入导出
    关于svn的dump和load命令
    Nginx多域名的泛解析
    kafka
    JavaScript
    失误与经验
    Hadoop
    Storm
  • 原文地址:https://www.cnblogs.com/sysout/p/5187146.html
Copyright © 2011-2022 走看看