@@ -171,59 +171,44 @@ public File newFile() throws IOException {
171
171
* folder.
172
172
*/
173
173
public File newFolder (String path ) throws IOException {
174
- if (new File (path ).isAbsolute ()) {
175
- throw new IOException ("folder path must be a relative path" );
176
- }
177
- File file = new File (getRoot (), path );
178
- if (!file .mkdirs ()) {
179
- if (file .isDirectory ()) {
180
- throw new IOException (
181
- "a folder with the path \' " + path + "\' already exists" );
182
- }
183
- throw new IOException (
184
- "could not create a folder with the path \' " + path + "\' " );
185
- }
186
- return file ;
174
+ return newFolder (new String []{path });
187
175
}
188
176
189
177
/**
190
- * Returns a new fresh folder with the given path under the temporary
178
+ * Returns a new fresh folder with the given paths under the temporary
191
179
* folder. For example, if you pass in the strings {@code "parent"} and {@code "child"}
192
180
* then a directory named {@code "parent"} will be created under the temporary folder
193
181
* and a directory named {@code "child"} will be created under the newly-created
194
182
* {@code "parent"} directory.
195
183
*/
196
184
public File newFolder (String ... paths ) throws IOException {
197
- File file = getRoot ();
185
+ // Before checking the paths, check if create() was ever called, and if it wasn't, throw
186
+ File root = getRoot ();
187
+
188
+ for (String path : paths ) {
189
+ if (new File (path ).isAbsolute ()) {
190
+ throw new IOException ("folder path \' " + path + "\' is not a relative path" );
191
+ }
192
+ }
193
+
194
+ File relativePath = null ;
195
+ File file = root ;
196
+ boolean lastMkdirsCallSuccessful = true ;
198
197
for (int i = 0 ; i < paths .length ; i ++) {
199
- String folderName = paths [i ];
200
- validateFolderName (folderName );
201
- file = new File (file , folderName );
202
- if (!file .mkdir () && isLastElementInArray (i , paths )) {
198
+ relativePath = relativePath == null ? new File (paths [i ]) : new File (relativePath , paths [i ]);
199
+ file = new File (root , relativePath .getPath ());
200
+
201
+ lastMkdirsCallSuccessful = file .mkdirs ();
202
+ if (!lastMkdirsCallSuccessful && !file .isDirectory ()) {
203
203
throw new IOException (
204
- "a folder with the name \' " + folderName + "\' already exists " );
204
+ "could not create a folder with the path \' " + relativePath . getPath () + "\' " );
205
205
}
206
206
}
207
- return file ;
208
- }
209
-
210
- /**
211
- * Validates if multiple path components were used while creating a folder.
212
- *
213
- * @param folderName
214
- * Name of the folder being created
215
- */
216
- private void validateFolderName (String folderName ) throws IOException {
217
- File tempFile = new File (folderName );
218
- if (tempFile .getParent () != null ) {
219
- String errorMsg = "Folder name cannot consist of multiple path components separated by a file separator."
220
- + " Please use newFolder('MyParentFolder','MyFolder') to create hierarchies of folders" ;
221
- throw new IOException (errorMsg );
207
+ if (!lastMkdirsCallSuccessful ) {
208
+ throw new IOException (
209
+ "a folder with the path \' " + relativePath .getPath () + "\' already exists" );
222
210
}
223
- }
224
-
225
- private boolean isLastElementInArray (int index , String [] array ) {
226
- return index == array .length - 1 ;
211
+ return file ;
227
212
}
228
213
229
214
/**
0 commit comments