JAVA-2097 Update "Rename File" article

This commit is contained in:
mikr
2020-07-14 14:11:12 +02:00
parent f57f769090
commit 663f9f2424
2 changed files with 65 additions and 0 deletions

View File

@@ -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));
}
}