@@ -7,7 +7,7 @@ package dotty.tools.io
7
7
8
8
import scala .language .implicitConversions
9
9
import java .io .RandomAccessFile
10
- import java .nio .file .{FileAlreadyExistsException , Files }
10
+ import java .nio .file .{DirectoryNotEmptyException , FileAlreadyExistsException , Files , NoSuchFileException }
11
11
import java .net .{URI , URL }
12
12
13
13
import scala .util .Random .alphanumeric
@@ -206,25 +206,23 @@ class Path private[io] (val jpath: JPath) {
206
206
207
207
// creations
208
208
def createDirectory (force : Boolean = true , failIfExists : Boolean = false ): Directory = {
209
- val res = try {
210
- if (force) Files .createDirectories(jpath) else Files .createDirectory(jpath)
211
- true
212
- } catch {
213
- case _ : FileAlreadyExistsException => false
214
- }
209
+ val res = tryCreate(if (force) Files .createDirectories(jpath) else Files .createDirectory(jpath))
215
210
if (! res && failIfExists && exists) fail(" Directory '%s' already exists." format name)
216
211
else if (isDirectory) toDirectory
217
212
else new Directory (jpath)
218
213
}
219
214
def createFile (failIfExists : Boolean = false ): File = {
220
- val res = try { Files .createFile(jpath); true } catch { case e : FileAlreadyExistsException => false }
215
+ val res = tryCreate( Files .createFile(jpath))
221
216
if (! res && failIfExists && exists) fail(" File '%s' already exists." format name)
222
217
else if (isFile) toFile
223
218
else new File (jpath)
224
219
}
225
220
221
+ private def tryCreate (create : => JPath ): Boolean =
222
+ try { create; true } catch { case _ : FileAlreadyExistsException => false }
223
+
226
224
// deletions
227
- def delete (): Unit = Files . delete(jpath)
225
+ def delete (): Unit = delete(jpath)
228
226
229
227
/** Deletes the path recursively. Returns false on failure.
230
228
* Use with caution!
@@ -234,12 +232,12 @@ class Path private[io] (val jpath: JPath) {
234
232
import scala .collection .JavaConverters ._
235
233
if (Files .isDirectory(p))
236
234
Files .list(p).iterator().asScala.foreach(deleteRecursively)
237
- try {
238
- Files .delete(p)
239
- true
240
- } catch { case _ : Throwable => false }
235
+ delete(p)
241
236
}
242
237
238
+ private def delete (path : JPath ): Boolean =
239
+ try { Files .delete(path); true } catch { case _ : DirectoryNotEmptyException | _ : NoSuchFileException => false }
240
+
243
241
def truncate () =
244
242
isFile && {
245
243
val raf = new RandomAccessFile (jfile, " rw" )
0 commit comments