eg:
TemplateCode.class.getClass().getClassLoader().getResource("foo/bar.txt")
TemplateCode.class.getClassLoader().getResource("foo/bar.txt")的区别
第二句话实际上还是调用的第一句,
区别在于第一句话不能以/开始,并总是从classpath的根目录开始查找,
第二句如果以/开始,则从根目录开始查找,如果开始不加/则从当前类所在的路径开始查找
验证通过
参考原文
http://stackoverflow.com/questions/14739550/what-is-the-difference-between-getclass-getclassloader-getresource-and-get
引用
The second one calls the first one. The difference is described in the javadoc.
The first one takes paths that don't start with a /, and always starts at the root of the classpath.
The second one takes path that can start with a /. If it does, it starts at the root of the classpath. If not, it starts at the package of the class on which the method is called.
So getClass().getClassLoader().getResource("foo/bar.txt") is equivalent to getClass().getResource("/foo/bar.txt").
And, assuming getClass() returns a class that is in the package foo, getClass().getResource("bar.txt") is equivalent to getClass().getClassLoader().getResource("foo/bar.txt")
分享到:
相关推荐
URLTest.class.getClassLoader().getResource().getPath(); Thread.currentThread().getContextClassLoader().getResource().getPath();等多种相似方式获得的路径,不能被FileReader()和FileWriter()直接应用...
URL url = Configuration.class.getClassLoader().getResource(filename); String str = url.getFile(); // 转换编码 str = URLDecoder.decode(str, "utf-8"); File conf = new File(str); ...
Digester digester = DigesterLoader.createDigester(TestDigester.class.getClassLoader().getResource(Constants.CONFIG_MAPPINGRULE_PATH)); digester.setValidating(false);
Image white= tk.getImage(cl.getResource("res/white.png")); Image black= tk.getImage(cl.getResource("res/black.png")); Image title= tk.getImage(cl.getResource("res/title.png")); Image temp; ...
Icon icon = new ImageIcon(ConfirmDialog.class.getClassLoader().getResource("info.png")); ScaleIcon scaleIcon = new ScaleIcon(icon); JLabel iconLabel = new JLabel(scaleIcon); iconLabel....
在websphere通过getClassLoader().getResource("").getPath()取配置文件的路径跟在tomcat环境下是不一样的,如果配置文件是放在classes目录下,会报找不到配置文件。 发布应用时,报web.xml文件有错 发布应用成功,...
String filepath = ContentMessage.class.getClassLoader().getResource("") .getPath(); filepath = filepath.substring(0, filepath.length() - 8); filepath = filepath + "resource/Application...
(1)、request.getRealPath(“/”);//不推荐使用获取工程的根路径 (2)、...//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用 (4)、 this.getClass().getClassLoader().getResource(“”).getPath();
testng-parser:一个简单的TestNG解析器TestNG Parser项目带有一个...URL url = cl.getResource("com/tupilabs/testng/parser/testng-results.xml");File file = new File(url.getFile());List<Suite> suites
im=tk.getImage(Tree.class.getClassLoader().getResource("images/dor.gif")); } public void draw(Graphics g) { if(!live) return; g.drawImage(im, x, y, null); if(r.nextInt(40) > 38){ //...
String fromRead=Dom4jTest2.class.getClassLoader().getResource("xml/map1.xml").getPath(); List<Pois> list=new ArrayList(); SAXReader saxReader = new SAXReader(); org.dom4j.Document document=...
Image white= tk.getImage(cl.getResource("res/white.png")); Image black= tk.getImage(cl.getResource("res/black.png")); Image title= tk.getImage(cl.getResource("res/title.png")); Image temp; JPanel...
Object obj = e.getSource(); if (obj == userItem || obj == userButton) { //用户信息设置 //调出用户信息设置对话框 UserConf userConf = new UserConf(this,userName); userConf.show(); ...
final ViewConfiguration configuration = ViewConfiguration.get(context); mTouchSlop = ViewConfigurationCompat .getScaledPagingTouchSlop(configuration); mMinimumVelocity = configuration....