0
0

More than 1 year has passed since last update.

Javaで「Pathオブジェクトを使ってファイルやディレクトリを表す」の動作を確認してみた

Posted at

概要

Javaで「Pathオブジェクトを使ってファイルやディレクトリを表す」の動作を確認してみました。以下のページを参考にしました。

実装

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

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

class JSample1_1{
  public static void main(String[] args){
    Path p1 = Paths.get("C:\\code\\java\\file\\report.txt");
    Path p2 = Paths.get("C:", "code", "java", "file", "report.txt");
    Path p3 = Paths.get("C:/code/java/file/report.txt");
    Path p4 = Paths.get("C:/code/java/file/");
    Path p5 = Paths.get("C:/code/java/file");

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

以下の通り実行できました。

$ javac JSample1_1.java
$ java JSample1_1 
C:\code\java\file\report.txt
C:/code/java/file/report.txt
C:/code/java/file/report.txt
C:/code/java/file
C:/code/java/file

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

JSample1_2.java
import java.nio.file.Path;

class JSample1_2{
  public static void main(String[] args){
    Path p1 = Path.of("C:\\code\\java\\file\\report.txt");
    Path p2 = Path.of("C:", "code", "java", "file", "report.txt");
    Path p3 = Path.of("C:/code/java/file/report.txt");

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

以下の通り実行できました。

$ javac JSample1_2.java 
$ java JSample1_2 
C:\code\java\file\report.txt
C:/code/java/file/report.txt
C:/code/java/file/report.txt

まとめ

何かの役に立てばと。

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