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

Posted on 2007-08-08
Last Modified: 2012-08-14
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.
Question by:SkyW4rrior
    LVL 1

    Expert Comment

    try this:

    String ctxPath = request.getContextPath();

    hth :)
    LVL 8

    Accepted Solution

    if you access the servlet Contex you can achieve the real path

    assume the you have servletContext

    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

    Author Comment

    Thank you very much. Worked fine!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now