Skip to content

Commit 2d69b93

Browse files
committed
Bug #29821
Bugfix for FileUtils.touch(). An existings file was overwritten. With new testcase. Submitted by: Christoffer Hammarstr�m <kreiger.at.linuxgods.com> git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140585 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9c0b607 commit 2d69b93

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
6565
* @author Matthew Hawthorne
6666
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
67-
* @version $Id: FileUtils.java,v 1.32 2004/06/13 04:58:07 bayard Exp $
67+
* @version $Id: FileUtils.java,v 1.33 2004/07/03 11:20:45 jeremias Exp $
6868
*/
6969
public class FileUtils {
7070

@@ -121,8 +121,9 @@ public static String byteCountToDisplaySize(long size) {
121121
* @throws IOException If an I/O problem occurs
122122
*/
123123
public static void touch(File file) throws IOException {
124-
OutputStream out = new java.io.FileOutputStream(file);
124+
OutputStream out = new java.io.FileOutputStream(file, true);
125125
IOUtils.closeQuietly(out);
126+
file.setLastModified(System.currentTimeMillis());
126127
}
127128

128129

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
*/
1616
package org.apache.commons.io;
1717

18-
import java.io.File;
19-
import java.io.FileInputStream;
20-
import java.io.IOException;
21-
import java.io.OutputStream;
18+
import java.io.*;
2219
import java.net.URL;
2320

2421
import org.apache.commons.io.testtools.FileBasedTestCase;
@@ -32,7 +29,7 @@
3229
*
3330
* @author Peter Donald
3431
* @author Matthew Hawthorne
35-
* @version $Id: FileUtilsTestCase.java,v 1.18 2004/04/23 22:47:39 jeremias Exp $
32+
* @version $Id: FileUtilsTestCase.java,v 1.19 2004/07/03 11:20:45 jeremias Exp $
3633
* @see FileUtils
3734
*/
3835
public class FileUtilsTestCase extends FileBasedTestCase {
@@ -450,4 +447,20 @@ public void testFileUtils() throws Exception {
450447

451448
}
452449

450+
public void testTouch() throws IOException {
451+
File file = new File(getTestDirectory(), "touch.txt") ;
452+
FileUtils.touch(file);
453+
assertTrue("FileUtils.touch() created file.", file.exists());
454+
FileOutputStream out = new FileOutputStream(file) ;
455+
assertEquals("Created empty file.", 0, file.length());
456+
out.write(0) ;
457+
out.close();
458+
assertEquals("Wrote one byte to file.", 1, file.length());
459+
file.setLastModified(0) ;
460+
assertEquals("Set lastModified to 0.", 0, file.lastModified());
461+
FileUtils.touch(file) ;
462+
assertEquals("FileUtils.touch() didn't empty the file.", 1, file.length());
463+
assertFalse("FileUtils.touch() changed lastModified.", 0 == file.lastModified()) ;
464+
}
465+
453466
}

0 commit comments

Comments
 (0)