From 31d314f7b5de22db089068de2923d9eddb5d7ef2 Mon Sep 17 00:00:00 2001 From: mikr Date: Tue, 14 Jul 2020 14:11:12 +0200 Subject: [PATCH] JAVA-2097 Update "Rename File" article --- core-java-modules/core-java-io/README.md | 1 + .../baeldung/rename/RenameFileUnitTest.java | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 core-java-modules/core-java-io/src/test/java/com/baeldung/rename/RenameFileUnitTest.java diff --git a/core-java-modules/core-java-io/README.md b/core-java-modules/core-java-io/README.md index dd0f4f58c3..c8a189ac81 100644 --- a/core-java-modules/core-java-io/README.md +++ b/core-java-modules/core-java-io/README.md @@ -12,4 +12,5 @@ This module contains articles about core Java input and output (IO) - [Getting a File’s Mime Type in Java](https://www.baeldung.com/java-file-mime-type) - [How to Avoid the Java FileNotFoundException When Loading Resources](https://www.baeldung.com/java-classpath-resource-cannot-be-opened) - [Create a Directory in Java](https://www.baeldung.com/java-create-directory) +- [Java – Rename or Move a File](https://www.baeldung.com/java-how-to-rename-or-move-a-file) - [[More -->]](/core-java-modules/core-java-io-2) diff --git a/core-java-modules/core-java-io/src/test/java/com/baeldung/rename/RenameFileUnitTest.java b/core-java-modules/core-java-io/src/test/java/com/baeldung/rename/RenameFileUnitTest.java new file mode 100644 index 0000000000..f4c4e99dac --- /dev/null +++ b/core-java-modules/core-java-io/src/test/java/com/baeldung/rename/RenameFileUnitTest.java @@ -0,0 +1,64 @@ +package com.baeldung.rename; + +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileSystemException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class RenameFileUnitTest { + + private final String FILE_TO_MOVE = "src/test/resources/originalFileToMove.txt"; + private final String TARGET_FILE = "src/test/resources/targetFileToMove.txt"; + + @BeforeEach + public void createFileToMove() throws IOException { + File fileToMove = new File(FILE_TO_MOVE); + fileToMove.createNewFile(); + } + + @AfterEach + public void cleanUpFiles() { + File targetFile = new File(TARGET_FILE); + targetFile.delete(); + } + + @Test + public void givenUsingNio_whenMovingFile_thenCorrect() throws IOException { + Path fileToMovePath = Paths.get(FILE_TO_MOVE); + Path targetPath = Paths.get(TARGET_FILE); + Files.move(fileToMovePath, targetPath); + } + + @Test + public void givenUsingFileClass_whenMovingFile_thenCorrect() throws IOException { + File fileToMove = new File(FILE_TO_MOVE); + boolean isMoved = fileToMove.renameTo(new File(TARGET_FILE)); + if (!isMoved) { + throw new FileSystemException(TARGET_FILE); + } + } + + @Test + public void givenUsingGuava_whenMovingFile_thenCorrect() + throws IOException { + File fileToMove = new File(FILE_TO_MOVE); + File targetFile = new File(TARGET_FILE); + + com.google.common.io.Files.move(fileToMove, targetFile); + } + + @Test + public void givenUsingApache_whenMovingFile_thenCorrect() throws IOException { + FileUtils.moveFile( + FileUtils.getFile(FILE_TO_MOVE), + FileUtils.getFile(TARGET_FILE)); + } + +}