@@ -7,7 +7,7 @@ package dotty.tools.io
77
88import scala .language .implicitConversions
99import java .io .RandomAccessFile
10- import java .nio .file .{DirectoryNotEmptyException , FileAlreadyExistsException , Files , NoSuchFileException }
10+ import java .nio .file .{DirectoryNotEmptyException , FileAlreadyExistsException , Files , NoSuchFileException , Paths }
1111import java .net .{URI , URL }
1212
1313import scala .util .Random .alphanumeric
@@ -45,20 +45,20 @@ object Path {
4545
4646 // not certain these won't be problematic, but looks good so far
4747 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 )
4949
5050 def onlyDirs (xs : Iterator [Path ]): Iterator [Directory ] = xs filter (_.isDirectory) map (_.toDirectory)
5151 def onlyDirs (xs : List [Path ]): List [Directory ] = xs filter (_.isDirectory) map (_.toDirectory)
5252 def onlyFiles (xs : Iterator [Path ]): Iterator [File ] = xs filter (_.isFile) map (_.toFile)
5353
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))
5555
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 ) }
6262
6363 /** Avoiding any shell/path issues by only using alphanumerics. */
6464 private [io] def randomPrefix = alphanumeric take 6 mkString " "
@@ -149,7 +149,7 @@ class Path private[io] (val jpath: JPath) {
149149 if (isAbsolute) toDirectory // it should be a root. BTW, don't need to worry about relative pathed root
150150 else Directory (" ." ) // a dir under pwd
151151 case x =>
152- Directory (x.toFile )
152+ Directory (x)
153153 }
154154 }
155155 def parents : List [Directory ] = {
@@ -213,6 +213,7 @@ class Path private[io] (val jpath: JPath) {
213213 }
214214 def createFile (failIfExists : Boolean = false ): File = {
215215 val res = tryCreate(Files .createFile(jpath))
216+ jfile.createNewFile()
216217 if (! res && failIfExists && exists) fail(" File '%s' already exists." format name)
217218 else if (isFile) toFile
218219 else new File (jpath)
@@ -236,7 +237,7 @@ class Path private[io] (val jpath: JPath) {
236237 }
237238
238239 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 }
240241
241242 def truncate () =
242243 isFile && {
0 commit comments