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

  • 相关阅读:
    移动采编app
    分布式自动化测试
    appium --log-timestamp > appium.log
    处理安卓的弹窗
    Sublime text3修改tab键为缩进为四个空格
    安卓自动化测试——rf
    敏捷软件开发
    photoshop怎么旋转图片
    thinkPHP5.0模型实现软删除
    thinkPHP5.0数据查询表达式生成技巧
  • 原文地址:https://www.cnblogs.com/sysout/p/5187146.html
Copyright © 2011-2022 走看看