Skip to content

Commit 0f77ce2

Browse files
author
Stephen Colebourne
committed
IO-107 - Add FileUtils.openOutputStream, creating parent directories if required
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@490858 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6295f65 commit 0f77ce2

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

RELEASE-NOTES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ Enhancements from 1.2
102102
- wildcardMatch - new method that has IOCase as a parameter
103103
- equals - new method that has IOCase as a parameter
104104

105+
- FileUtils.openOutputStream [IO-107]
106+
- new method to open a FileOutputStream, creating parent directories if required
107+
105108
- FileUtils.isFileOlder
106109
- add methods to check if a file is older (i.e. isFileOlder()) - counterparts
107110
to the existing isFileNewer() methods.

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,39 @@ public FileUtils() {
105105
*/
106106
public static final File[] EMPTY_FILE_ARRAY = new File[0];
107107

108+
//-----------------------------------------------------------------------
109+
/**
110+
* Opens a {@link FileOutputStream} for the specified file, checking and
111+
* creating the parent directory if it does not exist.
112+
* <p>
113+
* At the end of the method either the stream will be successfully opened,
114+
* or an exception will have been thrown.
115+
* <p>
116+
* The parent directory will be created if it does not exist.
117+
* The file will be created if it does not exist.
118+
* An exception is thrown if the file object exists but is a directory.
119+
* An exception is thrown if the parent directory cannot be created.
120+
*
121+
* @param file the file to create, not null
122+
* @throws IOException if the file object is a directory
123+
* @throws IOException if a parent directory needs creating but that fails
124+
*/
125+
public static FileOutputStream openOutputStream(File file) throws IOException {
126+
if (file.exists()) {
127+
if (file.isDirectory()) {
128+
throw new IOException("File '" + file + "' exists but is a directory");
129+
}
130+
} else {
131+
File parent = file.getParentFile();
132+
if (parent.exists() == false) {
133+
if (parent.mkdirs() == false) {
134+
throw new IOException("File '" + file + "' could not be created");
135+
}
136+
}
137+
}
138+
return new FileOutputStream(file);
139+
}
140+
108141
//-----------------------------------------------------------------------
109142
/**
110143
* Returns a human-readable version of the file size, where the input

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,55 @@ protected void tearDown() throws Exception {
102102
FileUtils.deleteDirectory(getTestDirectory());
103103
}
104104

105-
// byteCountToDisplaySize
105+
//-----------------------------------------------------------------------
106+
public void test_openOutputStream_exists() throws Exception {
107+
File file = new File(getTestDirectory(), "test.txt");
108+
createLineBasedFile(file, new String[] {"Hello"});
109+
FileOutputStream out = null;
110+
try {
111+
out = FileUtils.openOutputStream(file);
112+
out.write(0);
113+
} finally {
114+
IOUtils.closeQuietly(out);
115+
}
116+
assertEquals(true, file.exists());
117+
}
118+
119+
public void test_openOutputStream_existsButIsDirectory() throws Exception {
120+
File directory = new File(getTestDirectory(), "subdir");
121+
directory.mkdirs();
122+
try {
123+
FileUtils.openOutputStream(directory);
124+
fail();
125+
} catch (IOException ioe) {
126+
// expected
127+
}
128+
}
106129

130+
public void test_openOutputStream_notExists() throws Exception {
131+
File file = new File(getTestDirectory(), "a/test.txt");
132+
FileOutputStream out = null;
133+
try {
134+
out = FileUtils.openOutputStream(file);
135+
out.write(0);
136+
} finally {
137+
IOUtils.closeQuietly(out);
138+
}
139+
assertEquals(true, file.exists());
140+
}
141+
142+
public void test_openOutputStream_notExistsCannotCreate() throws Exception {
143+
File file = new File(getTestDirectory(), "a/:#$!/test.txt"); // empty path segment is bad directory name
144+
try {
145+
FileUtils.openOutputStream(file);
146+
fail();
147+
} catch (IOException ioe) {
148+
// expected
149+
}
150+
}
151+
152+
//-----------------------------------------------------------------------
153+
// byteCountToDisplaySize
107154
public void testByteCountToDisplaySize() {
108155
assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
109156
assertEquals(FileUtils.byteCountToDisplaySize(1024), "1 KB");

0 commit comments

Comments
 (0)