• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Opening a file with relative path

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to open a file (lets say "list.txt") inside a TagLibrary class file. My webapp folder organization under tomcat looks like this.



I have defined the file location in a properties file under config folder as "/resources/list.txt" hoping that the first "/" will default to the root of the webapp and it can find "list.txt" uner resources folder. But When I use the following code to open the file I am getting a file not found exception.



i did a bit of debugging to see whats happening and found that the value in " fileloc" is what I wanted "/resources/list.txt" but when I initialized the File object with that path, the listFile object has path variable set to "\\resources\\list.txt". i.e. it did not consider initial "/" as root of the webapp as I thought. So in this case what should I do to open this file. Since this is inside a TagLib class and not servlet I cannot use the request object to get the context path. Also I can give an absolute path in the properties file as my dev is in windows and prod is in unix. Help me fixing this for both the environments.

Thanks in advance.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use ServletContext.getRealPath to turn a path relative to the web application into an absolute path, then use that to construct a File object.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic