免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1544 | 回复: 0
打印 上一主题 下一主题

Find Resource file at Eclipse [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-07 14:59 |只看该作者 |倒序浏览
In Eclipse, at runtime, the following piece of code could not work


      Properties props = new Properties();
      try {
            props.load(new FileInputStream("test.properties"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
      }
        System.out.println(props.getProperty("test"));

Because eclipse can not recoginze the local file system path even you put the properties file at the same folder as ur java code, and also if we want to write portable Java Code, it is very bad to find resource file this way.
There are other 2 methods to find resource file, one of them is using URL, and the other method is putting resource files at class path, and write the code like below, since eclipse will put src folder at classpath, so it is also a safe way to write at Eclipse

    Properties props = new Properties();
    InputStream file =   
    ClassLoader.getSystemResourceAsStream("test.properties");
        
    try{
            props.load(file);
        } catch (IOException e) {
            e.printStackTrace();
   }
  System.out.println(props.getProperty("test"));

And if you want to read more about how to handle resource files smartly. Here is a link to a very good article at javaworld.com
http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/2956/showart_272676.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP