Fileと相対パス

Fileクラスには、"..(1つ上のディレクトリ)"とかも解決できる方法があったみたい。
前にAntのソースを見たときは自力でやっていた(ような気がする)ので、
デフォルトではできないんだと思ってた。


で、コードはこんな感じ。
単にFile#getCanonicalFileを使うだけ。


File file = new File("../hoge.txt");
File canonicalFile = file.getCanonicalFile();
ただし、IOExceptionを処理しなきゃいけない・・・。


おまけ。


File file1 = new File("abc.txt");
File file2 = new File("../abc.txt");
System.out.println(file1.getAbsolutePath());
System.out.println(file2.getCanonicalFile());

C:\tmp\abc.txt
C:\abc.txt