Skip to content

Commit 1bdcc3d

Browse files
committed
File copy operations now preserve the file date by default. An additional copyFile variant enables to disable this feature.
Suggested by: Craig Doremus <craig.at.maine.com> in Bugzilla #27615 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140542 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8d33a71 commit 1bdcc3d

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
7979
* @author Matthew Hawthorne
8080
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
81-
* @version $Id: FileUtils.java,v 1.28 2004/03/12 21:44:25 jeremias Exp $
81+
* @version $Id: FileUtils.java,v 1.29 2004/03/12 22:21:38 jeremias Exp $
8282
*/
8383
public class FileUtils {
8484

@@ -332,6 +332,7 @@ public static URL[] toURLs(File[] files) throws IOException {
332332
* Copy file from source to destination. If <code>destinationDirectory</code> does not exist, it
333333
* (and any parent directories) will be created. If a file <code>source</code> in
334334
* <code>destinationDirectory</code> exists, it will be overwritten.
335+
* The copy will have the same file date as the original.
335336
*
336337
* @param source An existing <code>File</code> to copy.
337338
* @param destinationDirectory A directory to copy <code>source</code> into.
@@ -350,13 +351,14 @@ public static void copyFileToDirectory(
350351
throw new IllegalArgumentException("Destination is not a directory");
351352
}
352353

353-
copyFile(source, new File(destinationDirectory, source.getName()));
354+
copyFile(source, new File(destinationDirectory, source.getName()), true);
354355
}
355356

356357
/**
357358
* Copy file from source to destination. The directories up to
358359
* <code>destination</code> will be created if they don't already exist.
359360
* <code>destination</code> will be overwritten if it already exists.
361+
* The copy will have the same file date as the original.
360362
*
361363
* @param source An existing non-directory <code>File</code> to copy
362364
* bytes from.
@@ -370,7 +372,31 @@ public static void copyFileToDirectory(
370372
* (use {@link #copyFileToDirectory}).
371373
*/
372374
public static void copyFile(File source, File destination)
373-
throws IOException {
375+
throws IOException {
376+
copyFile(source, destination, true);
377+
}
378+
379+
380+
/**
381+
* Copy file from source to destination. The directories up to
382+
* <code>destination</code> will be created if they don't already exist.
383+
* <code>destination</code> will be overwritten if it already exists.
384+
*
385+
* @param source An existing non-directory <code>File</code> to copy
386+
* bytes from.
387+
* @param destination A non-directory <code>File</code> to write bytes to
388+
* (possibly overwriting).
389+
* @param preserveFileDate True if the file date of the copy should be the
390+
* same as the original.
391+
*
392+
* @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be
393+
* written to, or an IO error occurs during copying.
394+
*
395+
* @throws FileNotFoundException if <code>destination</code> is a directory
396+
* (use {@link #copyFileToDirectory}).
397+
*/
398+
public static void copyFile(File source, File destination, boolean preserveFileDate)
399+
throws IOException {
374400
//check source exists
375401
if (!source.exists()) {
376402
String message = "File " + source + " does not exist";
@@ -410,6 +436,11 @@ public static void copyFile(File source, File destination)
410436
+ destination;
411437
throw new IOException(message);
412438
}
439+
440+
if (preserveFileDate) {
441+
//file copy should preserve file date
442+
destination.setLastModified(source.lastModified());
443+
}
413444
}
414445

415446
/**

src/test/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @author Peter Donald
3333
* @author Matthew Hawthorne
34-
* @version $Id: FileUtilsTestCase.java,v 1.12 2004/03/12 21:44:47 jeremias Exp $
34+
* @version $Id: FileUtilsTestCase.java,v 1.13 2004/03/12 22:22:25 jeremias Exp $
3535
* @see FileUtils
3636
*/
3737
public class FileUtilsTestCase extends FileBasedTestCase {
@@ -249,16 +249,41 @@ private void log(Object obj) {
249249

250250
public void testCopyFile1() throws Exception {
251251
File destination = new File(getTestDirectory(), "copy1.txt");
252+
253+
Thread.sleep(200); //This is to slow things down so we can catch if
254+
//the lastModified date is not ok
255+
252256
FileUtils.copyFile(testFile1, destination);
253257
assertTrue("Check Exist", destination.exists());
254258
assertTrue("Check Full copy", destination.length() == testFile1Size);
259+
assertTrue("Check last modified date preserved",
260+
testFile1.lastModified() == destination.lastModified());
255261
}
256262

257263
public void testCopyFile2() throws Exception {
258264
File destination = new File(getTestDirectory(), "copy2.txt");
265+
266+
Thread.sleep(200); //This is to slow things down so we can catch if
267+
//the lastModified date is not ok
268+
259269
FileUtils.copyFile(testFile1, destination);
260270
assertTrue("Check Exist", destination.exists());
261271
assertTrue("Check Full copy", destination.length() == testFile2Size);
272+
assertTrue("Check last modified date preserved",
273+
testFile1.lastModified() == destination.lastModified());
274+
}
275+
276+
public void testCopyFile2WithoutFileDatePreservation() throws Exception {
277+
File destination = new File(getTestDirectory(), "copy2.txt");
278+
279+
Thread.sleep(200); //This is to slow things down so we can catch if
280+
//the lastModified date is not ok
281+
282+
FileUtils.copyFile(testFile1, destination, false);
283+
assertTrue("Check Exist", destination.exists());
284+
assertTrue("Check Full copy", destination.length() == testFile2Size);
285+
assertTrue("Check last modified date modified",
286+
testFile1.lastModified() != destination.lastModified());
262287
}
263288

264289
// forceDelete
@@ -286,19 +311,31 @@ public void testCopyFile1ToDir() throws Exception {
286311
if (!directory.exists())
287312
directory.mkdirs();
288313
File destination = new File(directory, testFile1.getName());
314+
315+
Thread.sleep(200); //This is to slow things down so we can catch if
316+
//the lastModified date is not ok
317+
289318
FileUtils.copyFileToDirectory(testFile1, directory);
290319
assertTrue("Check Exist", destination.exists());
291320
assertTrue("Check Full copy", destination.length() == testFile1Size);
321+
assertTrue("Check last modified date preserved",
322+
testFile1.lastModified() == destination.lastModified());
292323
}
293324

294325
public void testCopyFile2ToDir() throws Exception {
295326
File directory = new File(getTestDirectory(), "subdir");
296327
if (!directory.exists())
297328
directory.mkdirs();
298329
File destination = new File(directory, testFile1.getName());
330+
331+
Thread.sleep(200); //This is to slow things down so we can catch if
332+
//the lastModified date is not ok
333+
299334
FileUtils.copyFileToDirectory(testFile1, directory);
300335
assertTrue("Check Exist", destination.exists());
301336
assertTrue("Check Full copy", destination.length() == testFile2Size);
337+
assertTrue("Check last modified date preserved",
338+
testFile1.lastModified() == destination.lastModified());
302339
}
303340

304341
// forceDelete

0 commit comments

Comments
 (0)