1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Javaで「2つのパスを結合する(Path.resolve,Path.resolveSibling)」の動作を確認してみた

Posted at

概要

Javaで「2つのパスを結合する(Path.resolve,Path.resolveSibling)」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

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

class JSample16_1{
  public static void main(String[] args){
    Path p1 = Paths.get("C:/data/image");
    Path p2 = Paths.get("photo/profile.jpg");
    Path p3 = p1.resolve(p2);

    System.out.println(p1);
    System.out.println(p2);
    System.out.println(p3);
  }
}

以下のコマンドを実行しました。

$ javac JSample16_1.java 
$ java JSample16_1 
C:/data/image
photo/profile.jpg
C:/data/image/photo/profile.jpg

以下のファイルを作成しました。

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

class JSample16_2{
  public static void main(String[] args){
    Path p1 = Paths.get("data/image/stone.jpg");
    Path p2 = Paths.get("grass.jpg");
    Path p3 = p1.resolveSibling(p2);

    System.out.println(p1);
    System.out.println(p2);
    System.out.println(p3);
  }
}

以下のコマンドを実行しました。

$ javac JSample16_2.java 
$ java JSample16_2 
data/image/stone.jpg
grass.jpg
data/image/grass.jpg

まとめ

何かの役に立てばと。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?