このチュートリアルを読んだところ、パスが参照するファイルが実際に存在する場合、toRealPath() は絶対パスを返す必要があります。
以下は、同じチュートリアルの抜粋です。
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)