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