如何从Java项目中的相对路径读取文件?java.io.File找不到指定的路径我有一个包含2个包的项目:tkorg.idrs.core.searchenginestkorg.idrs.core.searchengines在包(2)中我有一个文本文件ListStopWords.txt,在包(1)中我有一个类FileLoadder。这是代码FileLoader:File file = new File("properties\\files\\ListStopWords.txt");但有这个错误:The system cannot find the path specified你能解决一下吗?谢谢。
3 回答

Smart猫小萌
TA贡献1911条经验 获得超7个赞
我本可以评论,但我的代表较少。萨姆拉特的回答为我做了这个工作。最好通过以下代码查看当前目录路径。
File directory = new File("./"); System.out.println(directory.getAbsolutePath());
我只是用它来纠正我在项目中遇到的问题。请务必使用./返回当前目录的父目录。
./test/conf/appProperties/keystore

慕少森
TA贡献2019条经验 获得超9个赞
InputStream in = FileLoader.class.getResourceAsStream("<relative path from this class to the file to be read>");try { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); }} catch (Exception e) { e.printStackTrace();}
添加回答
举报
0/150
提交
取消