2013-04-29 4 views
1

이에서 방금 경로가 참조하는 파일이있는 경우 toRealPath()가 절대 경로를 제공해야한다고 읽었습니다. 여기 toRealPath(), IO/NIO 패키지 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")에 대한 내 바탕 화면에있는 기존 파일을 사용하는 경우; 그것은 존재하지 않는다면 저에게 예외를줍니다. 이 문제의 원인은 무엇입니까? 미리 감사드립니다.

EDIT : 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) 
+0

파일이 존재하고 올바른 경로/위치에 있음 – Rollerball

답변

1

의 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()); 
} 

당신은 구현에 따르면 여기 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/sun/nio/fs/UnixException.java#86

에서 전체 소스를 볼 수 NoSuchFileException가 throwed되고, ENOENT 오류 발생했다. 유닉스상의 ENOENT는 그러한 파일이나 디렉토리가 없다는 의미입니다.

"/home/user/Desktop/indeed.txt"파일이 있습니까? 액세스 권한이 있습니다.

사용하는 JDK의 버전이 무엇 /home/user/Desktop/indeed.txt

-l 명령 LS의 결과는 무엇인가?

+0

Java 버전 "1.7.0_21" Java (TM) SE 런타임 환경 (빌드 1.7.0_21-b11) – Rollerball

+0

및 -rw-rw-r-- 1 사용자 user 31 2013-04-25 16:52/home/user /Desktop/usnumbers.txt – Rollerball

+0

죄송합니다. 왜 이러한 일이 발생하는지 잘 모릅니다. 실행중인 사용자가 Java 프로그램을 실행하는 사용자가 실행중인 사용자와 동일합니까? –

1

정확하게 예외가 발생했는지 알려줄 수 있습니까? 언급 한 자습서에서 다음과 같이 말합니다.

이 메서드는 파일이 없거나 액세스 할 수없는 경우 예외를 발생시킵니다.

그래서 그 파일에 액세스 할 수 없습니다. JDK의 소스를 따라

+0

나는 예외의 printstacktrace를 사용하여 질문을 편집했습니다. – Rollerball