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で「ファイルやディレクトリを移動する(Files.move)」の動作を確認してみた

Posted at

概要

Javaで「ファイルやディレクトリを移動する(Files.move)」の動作を確認してみました。以下のページを参考にしました。

実装

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

JSample12_1.java
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;

class JSample12_1{
  public static void main(String[] args){
    Path p1 = Paths.get("/code/java/file/doc/memo.txt");
    Path p2 = Paths.get("/code/java/file/doc/memo.back");
    Path p3 = Paths.get("/code/java/file/doc/src");
    Path p4 = Paths.get("/code/java/file/doc/dest");

    try{
      Files.move(p1, p2);
      Files.move(p3, p4);
    }catch(IOException e){
      System.out.println(e);
    }
  }
}

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

$ javac JSample12_1.java
$ java JSample12_1

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

JSample11_1.java
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

class JSample11_1{
  public static void main(String[] args){
    Path p1 = Paths.get("/code/java/file/doc/memo.txt");
    Path p2 = Paths.get("/code/java/file/doc/memo.back");

    try{
      Files.move(p1, p2, REPLACE_EXISTING);
    }catch(IOException e){
      System.out.println(e);
    }
  }
}

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

$ javac JSample11_1.java 
$ java JSample11_1

まとめ

何かの役に立てばと。

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?