Skip to content

Commit b06408a

Browse files
author
Gary Gregory
committed
[IO-632] Add PathUtils for operations on NIO Path.
Refactor to add PathUtils.copyFileToDirectory(Path, Path, CopyOption...)
1 parent 154cb76 commit b06408a

8 files changed

Lines changed: 69 additions & 16 deletions

File tree

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public static PathCounters cleanDirectory(final Path directory) throws IOExcepti
4848
}
4949

5050
/**
51-
* Copies a source directory to a target directory.
51+
* Copies a directory to another directory.
5252
*
53-
* @param sourceDirectory The source directory
54-
* @param targetDirectory The target directory
53+
* @param sourceDirectory The source directory.
54+
* @param targetDirectory The target directory.
5555
* @param copyOptions Specifies how the copying should be done.
5656
* @return The visitation path counters.
5757
* @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
6363
sourceDirectory).getPathCounters();
6464
}
6565

66+
/**
67+
* Copies a file to a directory.
68+
*
69+
* @param sourceFile The source file
70+
* @param targetDirectory The target directory.
71+
* @param copyOptions Specifies how the copying should be done.
72+
* @return The target file
73+
* @throws IOException if an I/O error occurs
74+
* @see Files#copy(Path, Path, CopyOption...)
75+
*/
76+
public static Path copyFileToDirectory(final Path sourceFile, final Path targetDirectory,
77+
final CopyOption... copyOptions) throws IOException {
78+
return Files.copy(sourceFile, targetDirectory.resolve(sourceFile.getFileName()), copyOptions);
79+
80+
}
81+
6682
/**
6783
* Counts aspects of a directory including sub-directories.
6884
*

src/test/java/org/apache/commons/io/file/CleaningPathVisitorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.nio.file.Path;
2626
import java.nio.file.Paths;
2727

28-
import org.apache.commons.io.FileUtils;
2928
import org.apache.commons.io.file.Counters.PathCounters;
3029
import org.junit.jupiter.api.AfterEach;
3130
import org.junit.jupiter.api.Assertions;

src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919

2020
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
2121

22-
import java.io.File;
2322
import java.io.IOException;
2423
import java.nio.file.Files;
2524
import java.nio.file.Path;
2625
import java.nio.file.Paths;
2726
import java.nio.file.StandardCopyOption;
2827

29-
import org.apache.commons.io.FileUtils;
3028
import org.apache.commons.io.file.Counters.PathCounters;
3129
import org.junit.jupiter.api.AfterEach;
3230
import org.junit.jupiter.api.BeforeEach;

src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.nio.file.Path;
2525
import java.nio.file.Paths;
2626

27-
import org.apache.commons.io.FileUtils;
2827
import org.apache.commons.io.file.Counters.PathCounters;
2928
import org.junit.jupiter.api.AfterEach;
3029
import org.junit.jupiter.api.Assertions;

src/test/java/org/apache/commons/io/file/PathUtilsCleanDirectoryTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.nio.file.Path;
2626
import java.nio.file.Paths;
2727

28-
import org.apache.commons.io.FileUtils;
2928
import org.junit.jupiter.api.AfterEach;
3029
import org.junit.jupiter.api.BeforeEach;
3130
import org.junit.jupiter.api.Test;

src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.nio.file.Path;
2525
import java.nio.file.Paths;
2626

27-
import org.apache.commons.io.FileUtils;
2827
import org.junit.jupiter.api.AfterEach;
2928
import org.junit.jupiter.api.BeforeEach;
3029
import org.junit.jupiter.api.Test;

src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.nio.file.Path;
2626
import java.nio.file.Paths;
2727

28-
import org.apache.commons.io.FileUtils;
2928
import org.apache.commons.io.file.Counters.PathCounters;
3029
import org.junit.jupiter.api.AfterEach;
3130
import org.junit.jupiter.api.Assertions;
@@ -58,9 +57,8 @@ public void beforeEach() throws IOException {
5857
@Test
5958
public void testDeleteFileDirectory1FileSize0() throws IOException {
6059
final String fileName = "file-size-0.bin";
61-
FileUtils.copyFileToDirectory(
62-
Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName).toFile(),
63-
tempDir.toFile());
60+
PathUtils.copyFileToDirectory(
61+
Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName), tempDir);
6462
assertCounts(0, 1, 0, PathUtils.deleteFile(tempDir.resolve(fileName)));
6563
// This will throw if not empty.
6664
Files.deleteIfExists(tempDir);
@@ -72,9 +70,8 @@ public void testDeleteFileDirectory1FileSize0() throws IOException {
7270
@Test
7371
public void testDeleteFileDirectory1FileSize1() throws IOException {
7472
final String fileName = "file-size-1.bin";
75-
FileUtils.copyFileToDirectory(
76-
Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName).toFile(),
77-
tempDir.toFile());
73+
PathUtils.copyFileToDirectory(
74+
Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName), tempDir);
7875
assertCounts(0, 1, 1, PathUtils.deleteFile(tempDir.resolve(fileName)));
7976
// This will throw if not empty.
8077
Files.deleteIfExists(tempDir);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.file;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import java.io.IOException;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
27+
28+
import org.junit.jupiter.api.Test;
29+
30+
public class PathUtilsTest extends TestArguments {
31+
32+
@Test
33+
public void testCopyFile() throws IOException {
34+
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
35+
try {
36+
final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
37+
final Path targetFile = PathUtils.copyFileToDirectory(
38+
sourceFile, tempDir);
39+
assertTrue(Files.exists(targetFile));
40+
assertEquals(Files.size(sourceFile), Files.size(targetFile));
41+
} finally {
42+
PathUtils.deleteDirectory(tempDir);
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)