Converting Path to java File

90 views
Skip to first unread message

Stuart Roebuck

unread,
Sep 8, 2011, 10:18:38 AM9/8/11
to scala-i...@googlegroups.com
I'm sure I'm being daft, but how do you convert a Path to a java File or a PathSet to a set of Java Files?

My current code converts a Path to a File like this:

    new File(path.toURL.getFile)

and converts a PathSet to a Set[File] like this:

    pathSet.filter(_.isFile).map(p => new File(p.toURL.getFile)).toSet


Randall R Schulz

unread,
Sep 9, 2011, 12:27:21 AM9/9/11
to scala-i...@googlegroups.com

Look on the "Implicits" page linked form the File section / page of the
ScalaIO documentation.


Randall Schulz

Jesse Eichar

unread,
Sep 9, 2011, 2:09:42 AM9/9/11
to scala-i...@googlegroups.com
To be honest.  I have not given so much thought to the Path to file options.  The intention was to not have as little of the Java API in the API as possible.  One of the things I have on the list of TODOs before 0.2.0 is to make a single Conversions object for the major conversions to and from java so they are nicely contained in a single place. 

I will ensure that FIle -> Path -> is well supported.

Making a ticket to that effect.

Jesse

Jesse Eichar

unread,
Sep 9, 2011, 11:10:11 PM9/9/11
to scala-i...@googlegroups.com
I just realized, when taking  a closer look at Path that there is no direct conversion to File because Path does not necessarily have to do with the normal Filesystem.  It can be a path to a zip file entry for example.  However you can do:

path match {
  case p:DefaultPath => p.jfile
  case _ => ...
}

Jesse

Stuart Roebuck

unread,
Sep 14, 2011, 8:45:44 AM9/14/11
to scala-i...@googlegroups.com
Jesse,

Many thanks.

I've just used an implicit like this:

 implicit def defaultPathToFile(path: Path): File = {
    case path: DefaultPath => path.jfile
  }

Stuart Roebuck

unread,
Sep 14, 2011, 11:02:39 AM9/14/11
to scala-i...@googlegroups.com
Apologies, typo in last post:

  private implicit def defaultPathToFile(path: Path): File = path match {
Reply all
Reply to author
Forward
0 new messages