Netbeans:尝试加载文件但未找到(Java) [英] Netbeans: Try to load file but not found (Java)

查看:93
本文介绍了Netbeans:尝试加载文件但未找到(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在Netbeans(6.9)中使用Java加载文件时,我总是遇到相同的问题. 似乎找不到文件.我收到错误消息:

I have every time the same problem when I'm trying to load files with Java in Netbeans (6.9). It seems that the files aren't found. I get the error:

java.lang.NullPointerException

java.lang.NullPointerException

在这种情况下:

File file = new File(this.getClass().getClassLoader().getResource("file.xml").getFile());
// or this also don't work
File file = new File("file.xml");

文件file.xmlMain.java文件位于同一目录中. 我该如何加载该文件?

The file file.xml is in the same directory as the Main.java file. How could I load this file?

推荐答案

这应该有效(对我有用):

This should work (it does for me):


String path = URLDecoder.decode(getClass().getResource("file.xml").getFile(), "UTF-8");
File f = new File(path);

如果我正确理解Javadocs,这应该与使用getClass().getClassloader().getResource()相同,但是根据我的经验,这是不同的

If I understand the Javadocs correctly, this should be the same as using getClass().getClassloader().getResource() but in my experience it is different

这篇关于Netbeans:尝试加载文件但未找到(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆