how to read a file from a lift webapp
Asked Answered
M

2

5

I want to read xml file in my lift app:

val data = XML.load(new java.io.InputStreamReader(new java.io.FileInputStream(filename), encoding));

However, I am getting java.io.FileNotFoundException. where should I put the file, and what is the correct path from my scala code?

BTW - I am using embedded jetty for my testing, although I need a solution for dev env and production.

Milesmilesian answered 25/9, 2011 at 16:31 Comment(0)
T
7

There might be better solution for other paths but there is

var LiftRules.getResource : (String) ⇒ Box[URL]

or

def LiftRules.doWithResource(name: String)(f: (InputStream) ⇒ T): Box[T]

which usually point to files in src/main/resources/.

There is also

def LiftRules.loadResourceAsXml(name: String): Box[NodeSeq]

which may be the method you are looking for.

Tifanytiff answered 25/9, 2011 at 16:55 Comment(3)
When I tried this methods I got MalformedUrlException. I couldn't understand if the file content or the resource path was not formed well.Milesmilesian
Did you start the string with a slash (/myresource.res)?Tifanytiff
It work: LiftRules.loadResourceAsXml("/cities_hebrew_utf.xml"); Thanks!Milesmilesian
A
2

To solve this problem in the general case, new File("delme").createNewFile() and see where it ends up.

Alternate answered 25/9, 2011 at 16:49 Comment(4)
This may change depending on the directory the server was started in.Tifanytiff
Maybe, but for any particular configuration it will work, and it will work in Lift or Wicket or Play or...Alternate
It is working for me in my development environment. Hope it will work also when I will deploy it. Thanks!Milesmilesian
I just say that so everyone is aware of this potential problem. It has caused us a lot of headaches when our database file suddenly disappeared just because the server had been started manually.Tifanytiff

© 2022 - 2024 — McMap. All rights reserved.