zoukankan      html  css  js  c++  java
  • ClassLoaderTest

    package com.tms.apitest.common;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    public class ClassLoaderTest {
    
    	SetLog4j log = new SetLog4j();
    	
    	public String getProperties(String filename, String param) throws IOException {
            // 通过Class 获取资源  
    		
    		String propertiesValue = null;
    //        java.net.URL url = ClassLoaderTest.class.getResource("ClassLoaderTest.class");  
            //log.loggerInfo(url.toString());
            // 通过user.dir获取路径  
            String path = System.getProperty("user.dir");  
      
            Properties prop = new Properties();  
      
            // 通过File方式获取conf.properties  
            String confPath = path.concat(File.separator).concat("src")  
                    .concat(File.separator)
                    .concat("test").concat(File.separator).concat("resources")  
                    .concat(File.separator).concat(filename);  
            //log.loggerInfo(confPath);  
      
            File file = new File(confPath);  
            FileInputStream is = null;  
            if (file.exists() && !file.isDirectory()) {  
                try {  
                    is = new FileInputStream(file);  
                    prop.load(is);  
                    //log.loggerInfo("File Load Successful. " + param + " is: ["  
                    //        + prop.getProperty(param) + "]");  
                    propertiesValue = prop.getProperty(param);
                    prop.clear();  
                } catch (FileNotFoundException e) {  
                    e.printStackTrace();  
                    log.loggerInfo("File not Found Exception.");  
                } catch (Exception e) {  
                    e.printStackTrace();  
                    log.loggerInfo("Properties load Exception.");  
                } finally {  
                    if (is != null) {  
                        try {  
                            is.close();  
                        } catch (IOException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }  
            } else {  
            	log.loggerInfo("Can not find properties from [" + confPath  
                        + "]");  
            }  
            
    		return propertiesValue;  
    	}
    }
    

      

  • 相关阅读:
    yellow joke 3
    JavaScript下拉菜单(1)
    Java程序员面试三十二问
    Oracle里时间的应用
    ·Java读取properties文件
    Web 窗体用户控件
    怎么解决层被flash挡住的问题?
    自定义控件
    关于mysql中的DDL,DML,DQL和DCL
    自定义一个简单连接池以及常见连接池的使用
  • 原文地址:https://www.cnblogs.com/by170628/p/8134619.html
Copyright © 2011-2022 走看看