Add PathUtils for operations on NIO Path.
Refactor to add PathUtils.copyFileToDirectory(Path, Path, CopyOption...)
MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -48,10 +48,10 @@ public static PathCounters cleanDirectory(final Path directory) throws IOExcepti
}
/**
- * Copies a source directory to a target directory.
+ * Copies a directory to another directory.
*
- * @param sourceDirectory The source directory
- * @param targetDirectory The target directory
+ * @param sourceDirectory The source directory.
+ * @param targetDirectory The target directory.
* @param copyOptions Specifies how the copying should be done.
* @return The visitation path counters.
* @throws IOException if an I/O error is thrown by a visitor method.
@@ -63,6 +63,22 @@ public static PathCounters copyDirectory(final Path sourceDirectory, final Path
sourceDirectory).getPathCounters();
}
+ /**
+ * Copies a file to a directory.
+ *
+ * @param sourceFile The source file
+ * @param targetDirectory The target directory.
+ * @param copyOptions Specifies how the copying should be done.
+ * @return The target file
+ * @throws IOException if an I/O error occurs
+ * @see Files#copy(Path, Path, CopyOption...)
+ */
+ public static Path copyFileToDirectory(final Path sourceFile, final Path targetDirectory,
+ final CopyOption... copyOptions) throws IOException {
+ return Files.copy(sourceFile, targetDirectory.resolve(sourceFile.getFileName()), copyOptions);
+
+ }
+
/**
* Counts aspects of a directory including sub-directories.
*

MODIFY
src/test/java/org/apache/commons/io/file/CleaningPathVisitorTest.java
src/test/java/org/apache/commons/io/file/CleaningPathVisitorTest.java
import java.nio.file.Path;
import java.nio.file.Paths;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.file.Counters.PathCounters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;

MODIFY
src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java
src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
-import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.file.Counters.PathCounters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

MODIFY
src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java
src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java
import java.nio.file.Path;
import java.nio.file.Paths;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.file.Counters.PathCounters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsCleanDirectoryTest.java
src/test/java/org/apache/commons/io/file/PathUtilsCleanDirectoryTest.java
import java.nio.file.Path;
import java.nio.file.Paths;
-import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java
src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java
import java.nio.file.Path;
import java.nio.file.Paths;
-import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java
src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java
import java.nio.file.Path;
import java.nio.file.Paths;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.file.Counters.PathCounters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
@@ -58,9 +57,8 @@ public void beforeEach() throws IOException {
@Test
public void testDeleteFileDirectory1FileSize0() throws IOException {
final String fileName = "file-size-0.bin";
- FileUtils.copyFileToDirectory(
- Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName).toFile(),
- tempDir.toFile());
+ PathUtils.copyFileToDirectory(
+ Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName), tempDir);
assertCounts(0, 1, 0, PathUtils.deleteFile(tempDir.resolve(fileName)));
// This will throw if not empty.
Files.deleteIfExists(tempDir);
@@ -72,9 +70,8 @@ public void testDeleteFileDirectory1FileSize0() throws IOException {
@Test
public void testDeleteFileDirectory1FileSize1() throws IOException {
final String fileName = "file-size-1.bin";
- FileUtils.copyFileToDirectory(
- Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName).toFile(),
- tempDir.toFile());
+ PathUtils.copyFileToDirectory(
+ Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName), tempDir);
assertCounts(0, 1, 1, PathUtils.deleteFile(tempDir.resolve(fileName)));
// This will throw if not empty.
Files.deleteIfExists(tempDir);

ADD
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.file;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Test;
+
+public class PathUtilsTest extends TestArguments {
+
+ @Test
+ public void testCopyFile() throws IOException {
+ final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
+ try {
+ final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
+ final Path targetFile = PathUtils.copyFileToDirectory(
+ sourceFile, tempDir);
+ assertTrue(Files.exists(targetFile));
+ assertEquals(Files.size(sourceFile), Files.size(targetFile));
+ } finally {
+ PathUtils.deleteDirectory(tempDir);
+ }
+ }
+
+}

[IO-632] Add PathUtils for operations on NIO Path.

Refactor to add PathUtils.copyFileToDirectory(Path, Path, CopyOption...)