@@ -7,8 +7,12 @@ package dotty.tools.io
7
7
8
8
import scala .language .implicitConversions
9
9
import java .io .RandomAccessFile
10
- import java .nio .file .{ DirectoryNotEmptyException , FileAlreadyExistsException , Files , NoSuchFileException , Paths }
10
+ import java .nio .file ._
11
11
import java .net .{URI , URL }
12
+ import java .nio .file .attribute .BasicFileAttributes
13
+ import java .io .IOException
14
+
15
+ import scala .collection .JavaConverters ._
12
16
13
17
import scala .util .Random .alphanumeric
14
18
@@ -51,7 +55,7 @@ object Path {
51
55
def onlyDirs (xs : List [Path ]): List [Directory ] = xs filter (_.isDirectory) map (_.toDirectory)
52
56
def onlyFiles (xs : Iterator [Path ]): Iterator [File ] = xs filter (_.isFile) map (_.toFile)
53
57
54
- def roots : List [Path ] = java.io. File .listRoots ().toList .map(r => Path .apply(r.toPath))
58
+ def roots : List [Path ] = FileSystems .getDefault.getRootDirectories.iterator ().asScala .map(Path .apply).toList
55
59
56
60
def apply (path : String ): Path = apply(Paths .get(path))
57
61
def apply (jpath : JPath ): Path = try {
@@ -226,12 +230,21 @@ class Path private[io] (val jpath: JPath) {
226
230
/** Deletes the path recursively. Returns false on failure.
227
231
* Use with caution!
228
232
*/
229
- def deleteRecursively (): Boolean = deleteRecursively(jpath)
230
- private def deleteRecursively (p : JPath ): Boolean = {
231
- import scala .collection .JavaConverters ._
232
- if (Files .isDirectory(p))
233
- Files .list(p).iterator().asScala.foreach(deleteRecursively)
234
- delete(p)
233
+ def deleteRecursively (): Boolean = {
234
+ if (isDirectory) {
235
+ Files .walkFileTree(jpath, new SimpleFileVisitor [JPath ]() {
236
+ override def visitFile (file : JPath , attrs : BasicFileAttributes ) = {
237
+ Files .delete(file)
238
+ FileVisitResult .CONTINUE
239
+ }
240
+
241
+ override def postVisitDirectory (dir : JPath , exc : IOException ) = {
242
+ Files .delete(dir)
243
+ FileVisitResult .CONTINUE
244
+ }
245
+ })
246
+ true
247
+ } else false
235
248
}
236
249
237
250
private def delete (path : JPath ): Boolean =
0 commit comments