简体   繁体   English

toRealPath(),IO / NIO包Java

[英]toRealPath(), IO/NIO package Java

I have just read on this tutorial that toRealPath(), should give back the absolute path if the file that the path refers to really exists. 我刚刚在本教程上读到,如果路径引用的文件确实存在,则toRealPath()应该返回绝对路径。

Here is a snippet from the same tutorial: 这是同一教程的摘录:

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. }

So, now when I use an existing file located on my desktop for example ( Path inputPath = Paths.get("/home/user/Desktop/indeed.txt" ); It gives me an exception like if it did not exist. What may cause this problem? Thanks a lot in advance indeed. 因此,现在,例如,当我使用桌面上的现有文件时( Path inputPath = Paths.get("/home/user/Desktop/indeed.txt" );它给了我一个异常,就像它不存在一样。可能会导致此问题吗?确实非常感谢。

EDIT: I get a NoSuchFileException out of it. 编辑:我从中得到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)

Can you tell us the exact exception thrown? 您能告诉我们抛出的确切异常吗? As tutorial you mentioned says: 正如您提到的教程所述:

This method throws an exception if the file does not exist or cannot be accessed. 如果文件不存在或无法访问,则此方法将引发异常。

So it may be that you simply cannot access that file. 因此,可能是您根本无法访问该文件。

according the source of jdk, the translateToIOException method is implemented like this: 根据jdk的来源,translateToIOException方法是这样实现的:

private IOException translateToIOException(String file, String other) {
    // created with message rather than errno
    if (msg != null)
        return new IOException(msg);

    // handle specific cases
    if (errno() == UnixConstants.EACCES)
        return new AccessDeniedException(file, other, null);
    if (errno() == UnixConstants.ENOENT)
        return new NoSuchFileException(file, other, null);
    if (errno() == UnixConstants.EEXIST)
        return new FileAlreadyExistsException(file, other, null);

    // fallback to the more general exception
    return new FileSystemException(file, other, errorString());
}

You can view the whole source at here http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/sun/nio/fs/UnixException.java#86 您可以在以下位置查看整个源: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/sun/nio/fs/UnixException.java#86

According the implementation, when NoSuchFileException is throwed, an ENOENT error occured. 根据实现,抛出NoSuchFileException时,发生ENOENT错误。 ENOENT on unix stands for No such file or directory. Unix上的ENOENT代表没有这样的文件或目录。

Are you sure file "/home/user/Desktop/indeed.txt" exsits? 您确定文件“ /home/user/Desktop/indeed.txt”已存在吗? or you have privileges to access it. 或者您有权访问它。

What is the result of command ls -l /home/user/Desktop/indeed.txt 命令ls -l /home/user/Desktop/indeed.txt的结果是什么

what is the version of jdk you are using? 您正在使用的JDK版本是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM