« Return to Thread: Source.fromFile(file).getLines

Re: Source.fromFile(file).getLines

by Ricky Clarkson :: Rate this Message:

Reply to Author | View in Thread

ricky@ricky-desktop:~$ cat test.scala
import scala.io.Source
import java.io.File

object Main {
        def main(args: Array[String]) {
                for (line <- Source.fromFile(new File("test.scala")).getLines)
                        print("("+line+")")
        }
}


ricky@ricky-desktop:~$ scalac test.scala
ricky@ricky-desktop:~$ scala Main
(import scala.io.Source
)(import java.io.File
)(
)(object Main {
)(      def main(args: Array[String]) {
)(              for (line <- Source.fromFile(new File("test.scala")).getLines)
)(                      print("("+line+")")
)(      }
)(}
)(
)(
)

Note that I used print instead of println.  I can't imagine it's a
good thing to have line contain a line terminator of any kind.

2009/7/9 Daniel Sobral <dcsobral@...>:

> No, not expected. I don't get that either. Try doing map(_ map(_ toInt)) to
> the result of getLines. Maybe you are getting "\r\n" and confusing it with
> "\n\n".
>
> On Thu, Jul 9, 2009 at 1:38 PM, Gilles SCOUVART <Gilles.SCOUVART@...>
> wrote:
>>
>> Hello all,
>>
>> I was surprised to notice that using Source.fromFile(file).getLines on a
>> file with empty lines, those were added to the previous non empty line.
>> I.e. if the file contains this:
>> ------
>> test
>>
>>
>> ------
>> then getLines.next would return "test\n\n" (written java-echap-style).
>> I expected it would iterate on three lines instead: "test", "", and "".
>> This is a little bit annoying for pattern matching on full lines...
>>
>> Is this expected behaviour?
>>
>> Gilles Scouvart.
>
>
> --
> Daniel C. Sobral
>
> Something I learned in academia: there are three kinds of academic reviews:
> review by name, review by reference and review by value.
>



--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...

 « Return to Thread: Source.fromFile(file).getLines