commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r490858 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/FileUtils.java src/test/org/apache/commons/io/FileUtilsTestCase.java
Date Thu, 28 Dec 2006 22:50:08 GMT
Author: scolebourne
Date: Thu Dec 28 14:50:07 2006
New Revision: 490858

URL: http://svn.apache.org/viewvc?view=rev&rev=490858
Log:
IO-107 - Add FileUtils.openOutputStream, creating parent directories if required

Modified:
    jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java

Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?view=diff&rev=490858&r1=490857&r2=490858
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Thu Dec 28 14:50:07 2006
@@ -102,6 +102,9 @@
   - wildcardMatch - new method that has IOCase as a parameter
   - equals - new method that has IOCase as a parameter
 
+- FileUtils.openOutputStream  [IO-107]
+  - new method to open a FileOutputStream, creating parent directories if required
+
 - FileUtils.isFileOlder
   - add methods to check if a file is older (i.e. isFileOlder()) - counterparts
     to the existing isFileNewer() methods.

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?view=diff&rev=490858&r1=490857&r2=490858
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Thu Dec
28 14:50:07 2006
@@ -107,6 +107,39 @@
 
     //-----------------------------------------------------------------------
     /**
+     * Opens a {@link FileOutputStream} for the specified file, checking and
+     * creating the parent directory if it does not exist.
+     * <p>
+     * At the end of the method either the stream will be successfully opened,
+     * or an exception will have been thrown.
+     * <p>
+     * The parent directory will be created if it does not exist.
+     * The file will be created if it does not exist.
+     * An exception is thrown if the file object exists but is a directory.
+     * An exception is thrown if the parent directory cannot be created.
+     * 
+     * @param file  the file to create, not null
+     * @throws IOException if the file object is a directory
+     * @throws IOException if a parent directory needs creating but that fails
+     */
+    public static FileOutputStream openOutputStream(File file) throws IOException {
+        if (file.exists()) {
+            if (file.isDirectory()) {
+                throw new IOException("File '" + file + "' exists but is a directory");
+            }
+        } else {
+            File parent = file.getParentFile();
+            if (parent.exists() == false) {
+                if (parent.mkdirs() == false) {
+                    throw new IOException("File '" + file + "' could not be created");
+                }
+            }
+        }
+        return new FileOutputStream(file);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
      * Returns a human-readable version of the file size, where the input
      * represents a specific number of bytes.
      *

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?view=diff&rev=490858&r1=490857&r2=490858
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Thu Dec 28 14:50:07 2006
@@ -102,8 +102,55 @@
         FileUtils.deleteDirectory(getTestDirectory());
     }
 
-    // byteCountToDisplaySize
+    //-----------------------------------------------------------------------
+    public void test_openOutputStream_exists() throws Exception {
+        File file = new File(getTestDirectory(), "test.txt");
+        createLineBasedFile(file, new String[] {"Hello"});
+        FileOutputStream out = null;
+        try {
+            out = FileUtils.openOutputStream(file);
+            out.write(0);
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+        assertEquals(true, file.exists());
+    }
+
+    public void test_openOutputStream_existsButIsDirectory() throws Exception {
+        File directory = new File(getTestDirectory(), "subdir");
+        directory.mkdirs();
+        try {
+            FileUtils.openOutputStream(directory);
+            fail();
+        } catch (IOException ioe) {
+            // expected
+        }
+    }
+
+    public void test_openOutputStream_notExists() throws Exception {
+        File file = new File(getTestDirectory(), "a/test.txt");
+        FileOutputStream out = null;
+        try {
+            out = FileUtils.openOutputStream(file);
+            out.write(0);
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+        assertEquals(true, file.exists());
+    }
 
+    public void test_openOutputStream_notExistsCannotCreate() throws Exception {
+        File file = new File(getTestDirectory(), "a/:#$!/test.txt");  // empty path segment
is bad directory name
+        try {
+            FileUtils.openOutputStream(file);
+            fail();
+        } catch (IOException ioe) {
+            // expected
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    // byteCountToDisplaySize
     public void testByteCountToDisplaySize() {
         assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
         assertEquals(FileUtils.byteCountToDisplaySize(1024), "1 KB");



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message