Problem printing content of file with scala-io

101 views
Skip to first unread message

Markus Jais

unread,
May 24, 2012, 5:10:50 PM5/24/12
to scala-i...@googlegroups.com
Hello,

I am trying to learn scala-io and have trouble to print a file.

Here is my code with outputs in comment

val path = Path("sample.txt")  // does exist
println(path.exists) // prints "true"

val lines = path.lines()
println(lines.size) // prints "0"
  
val upper = lines.map(_.toUpperCase)
upper.foreach(println) // prints nothing
println(path.slurpString) // prints nothing

I also tried:
val sample = Resource.fromFile("sample.txt")
sample.lines().foreach(println) // prints nothing

The file does exist (and "exists" returns true"). It has 4 lines of text.

The example file is in the "src/main/resources/" folder. The output is the same when run
from sbt or within Eclipse. 

Maybe I haven't yet understood how scala-io works. 

Any hints on what I am doing wrong would be very welcome. I watched the scala-io presentation from Scala Days 2012 
and this looks fascinating and I want to learn and use it.

Regards,

Markus










Daniel Sobral

unread,
May 24, 2012, 8:18:18 PM5/24/12
to scala-i...@googlegroups.com
If you are typing out these commands on the REPL, you might be
emptying the iterators when the REPL prints its string representation.
--
Daniel C. Sobral

I travel to the future all the time.

Jesse Eichar

unread,
May 25, 2012, 2:36:53 AM5/25/12
to scala-i...@googlegroups.com
I have not had this issue.

I need a few details and have a few debug suggestions:

1.  try println(Path("sample.txt").size) to ensure that the library is finding the correct file.
2.  println(Path("sample.txt").slurpString) to see if the file can be read as a string
3.  println(Path("sample.txt).bytes mkString "") to see if the bytes can be read

Keep in mind that sample.txt has to be in the same directory as where you run scala you can do:

println(Path("sample.txt").toAbsolute.path)
println(Path("sample.txt").toRealPath.path)  // canonical path

finally try creating a file and reading it with a text editor.

Path("test.txt").write("Hello world")


What OS are you on?
What filesystem?
What Java version?

Jesse

Markus Jais

unread,
May 25, 2012, 5:15:13 PM5/25/12
to scala-i...@googlegroups.com
Hi Jesse,

thanks for the help.

I run Java 1.7.0_04 and Scala 2.9.2 with the latest (stable) Eclipse plugin or on the command line.

I tried an absolute path and it works (using the fromString method).

The "sample.txt" toAbsolute.path does not exist. It prints:

/home/markus/workspace-scala/scala-io-tests/sample.txt

But the real path is:
/home/markus/workspace-scala/scala-io-tests/src/main/resources/sample.txt

That is, Eclipse should find it because this should be in the classpath.

What confused me is that exists returns true on the file. size returns Some(0).

I looked at the source code and if I understand it correctly, exists delegates to java.io.File where exists 
returns true if there is such a file.

I will use the absolute path for testing and files outside the "src/main/resources" folder for now. I just
want to learn the API and understand how scala-io works.

Thanks for the great work. This is a great project.

Regards,

Markus




Von: Jesse Eichar <jesse....@gmail.com>
An: scala-i...@googlegroups.com
Gesendet: 8:36 Freitag, 25.Mai 2012
Betreff: Re: Problem printing content of file with scala-io

Daniel Sobral

unread,
May 25, 2012, 6:25:54 PM5/25/12
to scala-i...@googlegroups.com
Stuff on resources must be acquired through ClassLoader's getResource,
as far as I know.
(http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html)

Jesse Eichar

unread,
May 26, 2012, 10:47:16 AM5/26/12
to scala-i...@googlegroups.com
Daniel is correct.  Path(".") is the the directory where the JVM was started.  I plan on adding the concept to PWD to the scala-io filesystem in the future but for now it has the same semantics as java.io.File (for the most part).  So if you want to get something off the file system you need to use one of the factory methods on Resource (Resource.fromClasspath).  That will not give you the Path object but it will give you an Input object that will allow you to read the file (excepting some bug that I haven't encountered yet).

As for the size giving 0.  I would say that is a bug on my part.  I think the implementation is simply Some(file.length) where file is the java.io file.  I need to check for existence before returning the size I guess.  I file a bug and take a look.

Jesse

Markus Jais

unread,
May 26, 2012, 4:05:27 PM5/26/12
to scala-i...@googlegroups.com
I looked at the source of size and it looks correct:

def size = if(jfile.exists) Some(jfile.length()) else None

another questions. I got the source from github and cannot build it.

sbt test says:

sbt.ResolveException: unresolved dependency: com.github.jsuereth#sperformance_2.9.2;0.1: not found

do I have to add something to the build definitions? 

I would like to have a closer look at the sources and also generate the eclipse project with sbt (makes navigating the source easier).

Regards,

Markus

Gesendet: 16:47 Samstag, 26.Mai 2012

Jesse Eichar

unread,
May 27, 2012, 11:10:16 AM5/27/12
to scala-i...@googlegroups.com
you only need performance for running the performance tests.  I use a custom version of josh's performance that is not published anywhere, that is why it can't find the jar.  I would recommend just removing the performance project from the build.  To do so edit project/Build.scala and change

lazy val root:Project = Project("root", file(".")).
    aggregate(coreProject,fileProject,perfProject,webSiteProject).
    settings(sharedSettings ++ Seq(publishArtifact := false, name := "Scala IO") :_*)

to

lazy val root:Project = Project("root", file(".")).
    aggregate(coreProject,fileProject,webSiteProject).
    settings(sharedSettings ++ Seq(publishArtifact := false, name := "Scala IO") :_*)
 
(I just deleted the perfProject)

not 100 percent sure but I would guess that that should fix the issue.

Jesse

Markus Jais

unread,
May 28, 2012, 3:59:33 AM5/28/12
to scala-i...@googlegroups.com
Thanks, this works.

Markus

Gesendet: 17:10 Sonntag, 27.Mai 2012

Jesse Eichar

unread,
May 29, 2012, 2:05:36 AM5/29/12
to scala-i...@googlegroups.com
I haven't had a change to look at this yet but could it be that one of the actions has the side effect of creating the file.  For example I could see a bug where a FileChannel is opened to the file, maybe to check some aspect of the file and as a result the file is created, possibly empty and then the exists call works.

Or another idea, You tried Path("sample.txt").lines() and it failed but as a side effect it created an empty file.  Then you decided to debug and then exists and length calls works.  So in this scenario the bug would be the lines (or some other request) created the file (even though it was not supposed to).

(Just brainstorming some potential causes for the issue you observed)

Jesse

Markus Jais

unread,
May 30, 2012, 3:29:52 AM5/30/12
to scala-i...@googlegroups.com
That sounds like a reasonable explanation. Currently I don't have access to my machine at home but I 
will try again and see if files are created.

Markus

Gesendet: 8:05 Dienstag, 29.Mai 2012

Jesse Eichar

unread,
May 30, 2012, 5:40:08 AM5/30/12
to scala-i...@googlegroups.com
I am going to write some tests as well.  But right now I am working on getting scala-io to work with scala 2.10 so I can get it compiling with the nightly builds.  I have it compiling but I have had to comment out some important performance enhancements so I need to get those issues figured out as well as fixing some of the tests.  

Jesse

Markus Jais

unread,
May 30, 2012, 4:29:38 PM5/30/12
to scala-i...@googlegroups.com
Hi Jesse,

I did a quick test:

val lines = Path("dummy.txt").lines()

Did not create any files on my machine.

Hope this helps.

Markus

Gesendet: 11:40 Mittwoch, 30.Mai 2012

Jesse Eichar

unread,
May 30, 2012, 11:58:00 PM5/30/12
to scala-i...@googlegroups.com
Thanks marcus.  I am still going to add those tests so it doesn't start happening.

Andreas

unread,
Jul 16, 2012, 6:01:13 AM7/16/12
to scala-i...@googlegroups.com, Markus Jais
Hello,

I have a more general problem, I try to get scala.io running in eclipse.
What do I have to do for this?
I have downloaded the Source files and dropped them into an eclipse project, that simple approach does not seem to work since there are many comiple errors.
Can do hint where to find infromation how to do it correctly?

Thanks
Andreas

Jesse Eichar

unread,
Jul 16, 2012, 2:12:18 PM7/16/12
to scala-i...@googlegroups.com, Markus Jais
Are you trying to build scala.io in eclipse?  I usually use sbt to build an eclipse project.  That way it will get  the dependencies right.  Then import the projects into eclipse.

Jesse
Reply all
Reply to author
Forward
0 new messages