• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2173
  • Last Modified:

Java Web Application - Get the current dir / Read a text file

Does anyone know how can I read a text file from my JSF/ICEFACES website and put it content in an inputTextArea?
I'm doing this:
<ice:inputTextarea readonly="true" value="#{BCad.termos}"/>

where BCad is my backing bean and termos is the property that reads the textfile and return its content. The only way I found over the internet to read a text file is by using BufferedReader, that needs a FileReader which one needs the path to the file to be read. The problem is that I don't know what path it is in.. neither should know.. I want to give it like a web path in relationship to the root path of the website for example. e.g.: if the directory of the index is c:\mywebsite\index.jspx, and the text file is in c:\mywebsite\texts\mytext.txt so I should inform it as /texts/mytext.txt. Or it could be any way that I can give the current path of the website and use it to locate the text file; the way I found over the internet, to do this, is by System.getProperty("user.dir"); but this give me the path of the tomcat BIN directory.
0
SkyW4rrior
Asked:
SkyW4rrior
1 Solution
 
ayeenCommented:
try this:

String ctxPath = request.getContextPath();

hth :)
0
 
mnrzCommented:
if you access the servlet Contex you can achieve the real path

assume the you have servletContext
now
servletContext.getRealPath("/texts/mytext.txt");

will return the complete path of your file and you can load it by any File reader.

because you are using JSF I think you can get the servlet context by FacesContext object
0
 
SkyW4rriorAuthor Commented:
Thank you very much. Worked fine!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now