0
public class FilePath {
public File return_path() {
URL url = getClass().getResource("file.txt");
File file = new File(url.getPath());
return file;
}
}
如果我打印,输出的是这样的路径:“/媒体/日期/%20uni%C3%A0 /Java/project%20java%20201/SearchInFiles/build/classes/searchinfiles/hello.txt”
我也为了不重新定义每次,最终一个.jar将读取该文件的路径创造了这个方法。
有可能是一个奇怪的字符问题?
顺便说一下,当我把它从主类:
public static void main(String[] args) {
FilePath path = new FilePath();
File file = path.return_path();
System.out.println (file);
try{
BufferedReader input = new BufferedReader(new FileReader(file));
String line;
int i = 0;
while ((line = input.readLine()) != null)
{
System.out.println(line);
}
input.close();
}
catch(Exception ex){
System.err.println("Error: " + ex.getMessage());
}
}
我有“文件不存在”的错误。
我该如何解决? 谢谢
看一看右边的类似的问题.... – jlordo 2013-05-09 20:45:32
你为什么不使用'的getClass()。的getResourceAsStream(文件名) '直接? – hoaz 2013-05-09 20:51:17
这工作更好,谢谢 – Frank 2013-05-09 21:40:34