@@ -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 .{DirectoryNotEmptyException , FileAlreadyExistsException , Files , NoSuchFileException }
10
+ import java .nio .file .{DirectoryNotEmptyException , FileAlreadyExistsException , Files , NoSuchFileException , Paths }
11
11
import java .net .{URI , URL }
12
12
13
13
import scala .util .Random .alphanumeric
@@ -45,20 +45,20 @@ object Path {
45
45
46
46
// not certain these won't be problematic, but looks good so far
47
47
implicit def string2path (s : String ): Path = apply(s)
48
- implicit def jfile2path (jfile : JFile ): Path = apply(jfile)
48
+ implicit def jfile2path (jfile : JFile ): Path = apply(jfile.toPath )
49
49
50
50
def onlyDirs (xs : Iterator [Path ]): Iterator [Directory ] = xs filter (_.isDirectory) map (_.toDirectory)
51
51
def onlyDirs (xs : List [Path ]): List [Directory ] = xs filter (_.isDirectory) map (_.toDirectory)
52
52
def onlyFiles (xs : Iterator [Path ]): Iterator [File ] = xs filter (_.isFile) map (_.toFile)
53
53
54
- def roots : List [Path ] = java.io.File .listRoots().toList map Path .apply
54
+ def roots : List [Path ] = java.io.File .listRoots().toList. map(r => Path .apply(r.toPath))
55
55
56
- def apply (path : String ): Path = apply(new JFile (path))
57
- def apply (jfile : JFile ): Path = try {
58
- if (jfile.isFile) new File (jfile.toPath )
59
- else if (jfile .isDirectory) new Directory (jfile.toPath )
60
- else new Path (jfile.toPath )
61
- } catch { case ex : SecurityException => new Path (jfile.toPath ) }
56
+ def apply (path : String ): Path = apply(Paths .get (path))
57
+ def apply (jpath : JPath ): Path = try {
58
+ if (Files .isRegularFile(jpath)) new File (jpath )
59
+ else if (Files .isDirectory(jpath)) new Directory (jpath )
60
+ else new Path (jpath )
61
+ } catch { case ex : SecurityException => new Path (jpath ) }
62
62
63
63
/** Avoiding any shell/path issues by only using alphanumerics. */
64
64
private [io] def randomPrefix = alphanumeric take 6 mkString " "
@@ -149,7 +149,7 @@ class Path private[io] (val jpath: JPath) {
149
149
if (isAbsolute) toDirectory // it should be a root. BTW, don't need to worry about relative pathed root
150
150
else Directory (" ." ) // a dir under pwd
151
151
case x =>
152
- Directory (x.toFile )
152
+ Directory (x)
153
153
}
154
154
}
155
155
def parents : List [Directory ] = {
@@ -213,6 +213,7 @@ class Path private[io] (val jpath: JPath) {
213
213
}
214
214
def createFile (failIfExists : Boolean = false ): File = {
215
215
val res = tryCreate(Files .createFile(jpath))
216
+ jfile.createNewFile()
216
217
if (! res && failIfExists && exists) fail(" File '%s' already exists." format name)
217
218
else if (isFile) toFile
218
219
else new File (jpath)
@@ -236,7 +237,7 @@ class Path private[io] (val jpath: JPath) {
236
237
}
237
238
238
239
private def delete (path : JPath ): Boolean =
239
- try { Files .delete (path); true } catch { case _ : DirectoryNotEmptyException | _ : NoSuchFileException => false }
240
+ try { Files .deleteIfExists (path); true } catch { case _ : DirectoryNotEmptyException => false }
240
241
241
242
def truncate () =
242
243
isFile && {
0 commit comments