我刚刚阅读了这个tutorial toRealPath(),如果路径引用的文件确实存在,应该返回绝对路径。toRealPath(),IO/NIO package Java
下面是来自同一教程的一个片段:
try {
Path fp = path.toRealPath(); } catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
// Logic for case when file doesn't exist. } catch (IOException x) {
System.err.format("%s%n", x);
// Logic for sort of file error. }
所以,现在当我使用位于我的台式机例如(Path inputPath = Paths.get("/home/user/Desktop/indeed.txt"
)上的现有文件;它给了我一个例外,如果它不存在。 什么可能导致此问题? 的确非常感谢。
编辑:我得到一个NoSuchFileException它。
java.nio.file.NoSuchFileException: /home/user/Desktop/indeed.txt
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixPath.toRealPath(UnixPath.java:833)
at Pathss.main(Pathss.java:25)
显然该文件确实存在,并在正确的路径/位置 – Rollerball 2013-04-29 15:20:34