@@ -171,56 +171,43 @@ 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 ;
198
196
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 )) {
203
- throw new IOException (
204
- "a folder with the name \' " + folderName + "\' already exists" );
197
+ relativePath = relativePath == null ? new File (paths [i ]) : new File (relativePath , paths [i ]);
198
+ file = new File (root , relativePath .getPath ());
199
+ if (!file .mkdirs ()) {
200
+ if (!file .isDirectory ()) {
201
+ throw new IOException (
202
+ "could not create a folder with the path \' " + relativePath .getPath () + "\' " );
203
+ } else if (isLastElementInArray (i , paths )) {
204
+ throw new IOException (
205
+ "a folder with the path \' " + relativePath .getPath () + "\' already exists" );
206
+ }
205
207
}
206
208
}
207
209
return file ;
208
210
}
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 );
222
- }
223
- }
224
211
225
212
private boolean isLastElementInArray (int index , String [] array ) {
226
213
return index == array .length - 1 ;
0 commit comments