`

class.getClassLoader()路径问题

    博客分类:
  • Java
 
阅读更多

像通常的jdbc或者hibernate等配置文件时,我们要获得这些配置文件里的信息。我们一般用下面这个方法:

ClassName.class.getClassLoader().getResourceAsStream(file)这样就可以获得一个输入流,我们则可以通过这个输入流获得这个文件的信息了,但是如果你的文件放在包下面,则你须加上包的路径:如下格式net/sf/cindy/example/jdbc/fileName,其中net.sf.cindy.example.jdbc则为包明。千万别在前面加一个"/",实际上jvm都帮我们实现了这些,你可以通过class.getClassLoader().getResource("cindy.properties").getPath()打印发现这个就是在你编译下的跟目标下,我们加了一个"/"反而错了。根本都没那个目录,那肯定就找不到文件,那程序就抛异常了。

分享到:
评论

相关推荐

    SpringMVC源码分析系列

    NULL 博文链接:https://zzc1684.iteye.com/blog/2210510

    TangHanF#project_record_book#获取jar包所在路径1

    //得到windows下的正确路径// String path = monitorClientMain.class.getClassLoader().toStr

    Java路径问题解决方案汇集

     URLTest.class.getClassLoader().getResource().getPath();  Thread.currentThread().getContextClassLoader().getResource().getPath();等多种相似方式获得的路径,不能被FileReader()和FileWriter()直接应用...

    Mybatis源码分析.md

    InputStream inputStream = CommonTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml"); SQLSessionFactory SQLSessionFactory = new SQLSessionFactoryBuilder().build(inputStream)...

    pull解析XML

    List<WeatherInfos> infos = weatherService.getWeatherInfos(MainActivity.class.getClassLoader().getResourceAsStream("z.xml")); StringBuffer sb = new StringBuffer(); for(WeatherInfos info : infos){ ...

    传智博客jdbc学习案例

    InputStream inStream = DaoFactory.class.getClassLoader() .getResourceAsStream("daoconfig.properties"); prop.load(inStream); String userDaoClass = prop.getProperty("userDaoClass"); ...

    sax解析util

    URL url = Configuration.class.getClassLoader().getResource(filename); String str = url.getFile(); // 转换编码 str = URLDecoder.decode(str, "utf-8"); File conf = new File(str); ...

    Java 高级特性.doc

    //System.out.println(ProxyTest.class.getClassLoader().getParent().getClass().getName()); System.out.println(ProxyTest.class.getClassLoader().getClass().getName()); //test1(); //test2(); } ...

    java读取配置文件的实例代码

    java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。

    Jonny023#Study#Java-加载properties文件1

    1、使用class变量的getResourceAsStream()方法 2、使用class.getClassLoader()所得到的java.lang.Clas

    Digester解析xml

    Digester digester = DigesterLoader.createDigester(TestDigester.class.getClassLoader().getResource(Constants.CONFIG_MAPPINGRULE_PATH)); digester.setValidating(false);

    Java连接数据库代码

    in = MyTask.class.getClassLoader().getResourceAsStream("db.properties"); prop.load(in); this.sql_ip=prop.getProperty("DB_IP").trim(); this.sql_db_name=prop.getProperty("DB_NAME").trim(); this.sql...

    Java ClassLoader学习总结

    主要内容包括 Java类加载机制及加载流程,以及如何定义自己的类加载器,如何实现类的热替换。

    aspose-11.0.0.jar

    InputStream license = ChangePDF.class.getClassLoader().getResourceAsStream("\\license.xml"); try { new License().setLicense(license); } catch (Exception e) { e.printStackTrace(); } Document ...

    java工厂系列设计模式源码与文档

    Document document = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName)); Element root = document.getRootElement(); List list = XPath.selectNodes(root, "/beans/bean"); for...

    properties资源文件utils类

    String filepath = ContentMessage.class.getClassLoader().getResource("") .getPath(); filepath = filepath.substring(0, filepath.length() - 8); filepath = filepath + "resource/Application...

    测试 test-spring-spi

    List<Say> loadFactories = SpringFactoriesLoader.loadFactories(Say.class, TestCase.class.getClassLoader()); for (int i = 0; i < loadFactories.size(); i++) { Say say = loadFactories.get(i); ...

    java自定义弹窗Demo

    Icon icon = new ImageIcon(ConfirmDialog.class.getClassLoader().getResource("info.png")); ScaleIcon scaleIcon = new ScaleIcon(icon); JLabel iconLabel = new JLabel(scaleIcon); iconLabel....

    commons-dbcp2-2.0-bin.zip

    InputStream inStream = DBCPConnection.class.getClassLoader().getResourceAsStream("dbcp.properties"); propreties.load(inStream); DataSource dataSource = BasicDataSourceFactory.createDataSource...

    调用webservice例子 查询手机号归属地 soap协议的传xml

    InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); try { addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); } catch...

Global site tag (gtag.js) - Google Analytics