Accessing text file from a bean.

Posted on 2006-05-26
Last Modified: 2010-04-01

I'm trying to access a text file from a bean.
Here's my directory structure:
C:/tomcat/webapps/  ----> I have all the jsp pages here.
C:/tomcat/webapps/WEB-INF/classes ----> I have a java file where I'm trying to acces access the text file in C:/tomcat/webapps/WEB-INF/classes/index/myTextFile.txt

This is what I do in the java code:
String fileName = "index/myTextFile.txt";
FileReader fileReader = new FileReader(fileName);
But it doesn't find myTextFile.txt.
When I put the whole path into fileName: C:/tomcat/webapps/WEB-INF/classes/index/myTextFile.txt, it works. But I actually don't want this since I should put the code somewhere else.

I've also tried to do this and this doesn't work either:
String fileName = "index/myTextFile.txt";
File myFile = new File(fileName);
String absolutePath = myfile.getAbsolutePath();
FileReader fileReader = new FileReader(absolutePath);

Could someone please advice?

Question by:rnicholus
    LVL 27

    Accepted Solution

    First of all, you must put your bean in a package.  
    To access the file, try using  
    URL fileURL = getClass().getClassLoader().getResource("index/myTextFile.txt");
    FileReader fr = new FileReader(fileURL.getPath());

    Author Comment

    Thanks! It works.

    I think the getClass() method is not a static method so I did this:
    URL fileURL = Class.forName("beans.myClassName").getClassLoader().getResource("index/myTextFile.txt");
    FileReader fr = new FileReader(fileURL.getPath());
    LVL 27

    Expert Comment

    >I think the getClass() method is not a static method  
    You are right. But  
    >URL fileURL = getClass().getClassLoader().getResource("index/myTextFile.txt");  
     is the same as  
    URL fileURL = this.getClass().getClassLoader().getResource("index/myTextFile.txt");

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Digital marketing agencies have encountered both the opportunities and difficulties that emerge from working with a wide-ranging organizations.
    For Sennheiser, comfort, quality and security are high priority areas. This paper addresses the security of Bluetooth technology and the supplementary security that Sennheiser’s Contact Center and Office (CC&O) headsets provide.  
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    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

    12 Experts available now in Live!

    Get 1:1 Help Now