• Post Reply Bookmark Topic Watch Topic
  • New Topic

Openning a data file using relational path  RSS feed

 
Siamak Saarmann
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I am trying to open a data file ( a header and footer and sometimes other files ) using a function I have written:



But now that I have moved to netbeans I am not able to find how can I address a data file using relational paths.

I have copied data files to every directory! but it can not open the file.

1- Where should I put the files and with which relational path I will b able to access it?

2- Is there a way we can find the real physical path of a servlet? (as they may be in war files , is it possible)? I want to find the physical path of the servlet and then put data files in a relational path to it to solve my problem.

Regards,
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a common problem. The main thing to remember is that the files in your deployed web application may not even exist in the file system. It is quite feasible (although uncommon) for an application server to run your application straight from a war file, without ever "expanding" it. Even when the files do physically exist, different servers and contrainers are free to place them wherever they like. Relying on files to be wherever one server "just happened" to place them is very risky.

With this in mind, you have two main options:

  • Place your files at some well-known location ("C:/temp/files/", or "/home/whatever", for example), and use the full path when you open them.
  • Place your data files alongside your class files and load them using getResourceAsStream().


  • Which of these you choose is up to you.
     
    Siamak Saarmann
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much.

    I put a line of code in my web.xml:

    Set.incDir= config.getInitParameter("incDir");

    Which contains the physical path to my data files. Then I addressed the files by their physical paths.

    Thank you again for your help. This forum is great.

    Regards,
    Mac
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!