- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[newbie] problem reading a file
Fri, 2010-02-19, 19:27
What happened here?
Welcome to Scala version 2.8.0.r20879-b20100214020153 (Java
HotSpot(TM) Server VM, Java 1.6.0_14).
[...]
scala> import scala.io.Source
import scala.io.Source
scala> val file = "/home/stephan/lukits.de/theditor/settings.res"
file: java.lang.String = /home/stephan/lukits.de/theditor/settings.res
scala> val lines = Source.fromFile(file).getLines.toList
:6: error: type mismatch;
found : java.lang.String
required: java.io.File
Error occured in an application involving default arguments.
val lines = Source.fromFile(file).getLines.toList
^
scala>
Thanks for any advice and best regards, Stephan.
Fri, 2010-02-19, 20:17
#2
Re: [newbie] problem reading a file
In 2.8, use fromPath if you pass a String, and fromFile if you pass a java.io.File.
--Rex
On Fri, Feb 19, 2010 at 1:27 PM, Stephan Lukits <stephan@lukits.de> wrote:
--Rex
On Fri, Feb 19, 2010 at 1:27 PM, Stephan Lukits <stephan@lukits.de> wrote:
What happened here?
Welcome to Scala version 2.8.0.r20879-b20100214020153 (Java
HotSpot(TM) Server VM, Java 1.6.0_14).
[...]
scala> import scala.io.Source
import scala.io.Source
scala> val file = "/home/stephan/lukits.de/theditor/settings.res"
file: java.lang.String = /home/stephan/lukits.de/theditor/settings.res
scala> val lines = Source.fromFile(file).getLines.toList
<console>:6: error: type mismatch;
found : java.lang.String
required: java.io.File
Error occured in an application involving default arguments.
val lines = Source.fromFile(file).getLines.toList
^
scala>
Thanks for any advice and best regards, Stephan.
Fri, 2010-02-19, 20:47
#3
[solved] Re: [newbie] problem reading a file
Rex Kerr schrieb:
> In 2.8, use fromPath if you pass a String, and fromFile if you pass a
> java.io.File.
Thanks, Rex. I was reading the 2.7 api doc. Now I found
the 2.8 api doc which told me that toLine needs also a
line separator string; thinks work out so far.
regards Stephan
Fo some reason the version of fromFile that takes a File, not a String, is being used. See http://www.scala-lang.org/docu/files/api/scala/io/Source$object.html#fromFile%28java.io.File%29 versus http://www.scala-lang.org/docu/files/api/scala/io/Source$object.html#fromFile%28String%29/. I'm afraid I can't tell you off the top of my head why this is.
Peter Robinett
On Fri, Feb 19, 2010 at 10:27 AM, Stephan Lukits <stephan@lukits.de> wrote: