This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

FileReader - file not found. What am I missing?  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I want to pull in an xml file, however I seem to be having difficulty with the relative path. The file is on the root of src and is therefore replicated into the root of classes, however this fails indicating the file reference is invalid.

I am within a war (within an ear) however I am not in a servlet so I cannot take advantage of servletContext.

The following code fails indicating file not found. Any suggestions?


String path = "/WEB-INF/classes/";

String fileName = "SearchCriteria.xml";

try {
Reader reader = new FileReader(path + fileName);
.
.
.

This is on WebSphere 6.0, jdk 1.4 (yes this is old, but I am promised a shiny new WAS 7.0 soon).


Thanks,
Stu
 
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try following code to read the data file (XML, txt, properties etc) present in WAR or JAR file.

 
Marshal
Posts: 67197
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak's approach of using getResourceAsStream() is preferred over "normal" file I/O for two reasons:

  • If the web app is deployed as a war file, there is no file system in the first place.
  • Web app's have no concept of a "current directory", so relative addressing of file paths is not possible.

  • If you must use file I/O for some reason (and aren't deployed as a war file), you need to find the absolute file path with ServletContent.getRealPath().
     
    Ranch Hand
    Posts: 1179
    Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Deepak Pant wrote:You can try following code to read the data file (XML, txt, properties etc) present in WAR or JAR file.



    Why would you use "/WEB-INF/classes" in the getResourceAsStream path ??
     
    Bear Bibeault
    Marshal
    Posts: 67197
    169
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Rene Larsen wrote:Why would you use "/WEB-INF/classes" in the getResourceAsStream path ??


    That would be an odd place to put an XML file. Config files would customarily be placed in WEB-INF, or a folder under it (other than classes or lib).
     
    Rene Larsen
    Ranch Hand
    Posts: 1179
    Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:

    Rene Larsen wrote:Why would you use "/WEB-INF/classes" in the getResourceAsStream path ??


    That would be an odd place to put an XML file. Config files would customarily be placed in WEB-INF, or a folder under it (other than classes or lib).


    The /WEB-INF/classes folder can be used - but the path used for getting the xml file, was what I meant

    Instead of:
    I would use:
     
    Deepak Pant
    Ranch Hand
    Posts: 446
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think the exact location of the xml file was not the problem. I wanted to show sample code on how to access files bundled in WAR or JAR so I kept the same path as what Stu had in the first post.
     
    Bear Bibeault
    Marshal
    Posts: 67197
    169
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah yes, the difference between Class.getResourceAsStream() and ServletContext.getResourceAsStream().

    Using the former requires the file be on the classpath; the latter as relative form the context root.
     
    Deepak Pant
    Ranch Hand
    Posts: 446
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes thats true as well. Also "WEB-INF/classes/" is in the classpath so keeping "SearchCriteria.xml" file in there will make it easier.
     
    Stu Quinn
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for your recommendations. This works great. I had originally placed the file in \classes because there are some other related files at the root of \src and hence are copied to \classes. Your comments are right on and I will move my file to \WEB-INF.

    I really appreciate the help.

    Back in the saddle,
    Stu
     
    She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
    https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!