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

Re: Source.fromFile(file).getLines

by Josh Suereth :: Rate this Message:

Reply to Author | View in Thread

We're actively working on an I/O layer for scala in the Scalax project.  The 2.8.0 branch (on github) contains a method for handling different types of line endings.

You should see these test files:
http://github.com/eengbrec/Scalax.IO/blob/7dd08918223e7044bdd218fa075196160ae67b28/src/test/scala/scalax/io/TestStreams.scala
http://github.com/eengbrec/Scalax.IO/blob/7dd08918223e7044bdd218fa075196160ae67b28/src/test/scala/scalax/io/TestManagedStreams.scala

For an Idea on usage.   You should be able to use the File abstraction to pull in lines/chars/bytes/objects.

We're in the process of development now, but any feedback on the approach would be greatly appreciated!  We hope to have releasable code with the scala 2.8.0 release.


- Josh

On Thu, Jul 9, 2009 at 1:55 PM, Daniel Sobral <dcsobral@...> wrote:
How would you tell apart a line that ends in "\n" with one that doesn't? The method getLines doesn't strip away any characters present in the file. Which, by the way, is pretty standard behavior. In Perl, for instance, you need to "chomp" away newlines if you don't want them. Likewise, in Scala, you need to stripLineEnd newlines if you don't want them.


On Thu, Jul 9, 2009 at 2:07 PM, Ricky Clarkson <ricky.clarkson@...> wrote:
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@...



--
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.

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