This page is no longer maintained — Please continue to the home page at www.scala-lang.org

[newbie] problem reading a file

3 replies
Stephan Lukits
Joined: 2010-02-19,
User offline. Last seen 42 years 45 weeks ago.

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.

Peter Robinett
Joined: 2009-09-16,
User offline. Last seen 42 years 45 weeks ago.
Re: [newbie] problem reading a file
Hi 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:
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.

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
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:
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.

Stephan Lukits
Joined: 2010-02-19,
User offline. Last seen 42 years 45 weeks ago.
[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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland