LoginSignup
1
1

More than 5 years have passed since last update.

Javaで2つのFileクラス間の相対パスを取得する

Last updated at Posted at 2016-05-20

ぐぐっても検索トップの方に絶対パスから相対パスに変換する方法が出ないので、メモ代わりに書いておきます。

import java.io.File;

File child = new File("hoge/fuga/foo/bar.txt");
File parent = new File("hoge/fuga");

String relativePath = parent.toURI().relativize(child.toURI()).toString();
System.out.println(relativePath); // prints "foo/bar.txt"

以上

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1