0
0

More than 1 year has passed since last update.

【JavaGold】resolveメソッド

Posted at

概要

java.nio.file.Pathsクラスのresolve()メソッドは、2つのPathインスタンスを結合して新しいPathインスタンスを作成する。
このメソッドは、パスの絶対・相対の関係にかかわらず、2つのパスを連結して新しいパスを作成する。

シグネチャは以下のようになっている。

public Path resolve(Path other)

引数として結合したいもう一方のPathインスタンス(other)を受け取り、それらを結合した新しいPathインスタンスを返す。

例えば、次のようなディレクトリ構造があったとする。

root
├── dir1
│   └── file1.txt
└── dir2
    └── file2.txt

以下は、Paths.resolve()メソッドを使用して2つのPathインスタンスを結合している。

import java.nio.file.Path;
import java.nio.file.Paths;

public class ResolveExample {
    public static void main(String[] args) {
        Path dirPath = Paths.get("root/dir1");
        Path filePath = Paths.get("root/dir2/file2.txt");

        // ディレクトリパスとファイルパスを結合して新しいパスを作成
        Path resolvedPath = dirPath.resolve(filePath);

        System.out.println("結合されたパス: " + resolvedPath);
    }
}

この例では、Paths.get()メソッドを使って2つのPathインスタンス(dirPathfilePath)を作成している。dirPath.resolve(filePath)を呼び出すことで、dirPathfilePathを結合して新しいPathインスタンスを作成し、結合されたパスを出力している。

実行結果
結合されたパス: root/dir1/root/dir2/file2.txt

resolve()メソッドにより、dirPathfilePathが結合されて新しいパスが作成されていることがわかる。

0
0
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
0
0