I am trying to read a short file one line at a time. I know how to
iterate over the lines, but that is not what I want to do now. I
simply want to read three or four lines one at a time (using readLine
or some such method) and extract a different kind of information from
each line. I asked about this a couple of years ago, and I was told to
use scala.io.Source.fromPath. However, fromPath does not seem to exist
anymore.
Russ P.
Thanks, but that's way too complicated. I can do it trivially in
Python in a line or two. After screwing around for a while, I came up
with something like this:
val data =
scala.io.Source.fromFile("myFile.dat").getLines.toArray
I can then access the lines separately as elements of an Array (or I
could have used a List). That's not too bad, but for crying out loud
shouldn't I be able to just write
val file = scala.io.Source.fromFile("myFile.dat")
file.getLine
...
Actually, I'd prefer a getLine that just reads the next line rather
than taking an index argument.
--Russ P.
> Typesafe <http://www.typesafe.com/> - The software stack for applications
> that scale
>
> Twitter: @viktorklang
>
That looks good to me ... so why is getLine deprecated?
Actually, I'd prefer a getLine that just reads the next line rather
than taking an index argument.
val file = scala.io.Source.fromFile("myFile.dat")
val lines = file.getLines
val line = lines.next // remember, it's an Iterator!
--
Daniel C. Sobral
I travel to the future all the time.
whatever you do before, don't forget to close the source in a long running program.
Normally the operating system gives a file handle to the program
reading the file, so it knows to prevent other programs from modifying
the file or deleting the file while it is still being read. By
closing the input file, your program signals to the operating system
that it is finished using the file and other programs can change it.
-Mike