views:

29

answers:

2

Hello,

I have a Java project which has this file structure (shown in Eclipse):

ProjectName
+- Deployment Descriptor: ProjectName
¦- Java Resources:src
   ¦- Package1
      -MyClass.java
¦- FileFolder
   -MyFile.txt

And so far from myClass I'm able to read MyFile.txt using:

try
{
    reader = new BufferedReader(new FileReader(new File("FileFolder/MyFile.txt")));

while((line=reader.readLine())!=null)
{
    line=line.trim();
    myVector.add(line);
}
reader.close();
}
catch(Exception e)
{
     e.printStackTrace();
}

But when I put Package1 into a Dynamic Web Project AND the FileFolder folder in root, the file is no longer found.

Does anyone know how to read the file?

Thanks in advance!

A: 

You are opening a file using a path relative to the current working directory. That's not likely to work on a web app container because the current working directory will not be the root of your application.

Furthermore, you file might not be on the file system but rather in a WAR file.

The proper way to open a file in a webapp is to use the ServletContext.getResourceAsStream() method.

gawi
Interesting, I didn't know `ServletContext` had a `getResourceAsStream`.
R. Bemrose
Indeed, this is the way to access all resources in the WAR (even what's under /WEB-INF/). ServletContext.getResourcePaths() acts a bit like File.list() as it list the content of the directory.Note that it is also possible to use File class to access files on the file system that reside outside the web application. In the latter case, you must use an absolute path to locate your file.
gawi
Actually, yes. I found that using a getClassLoader worked for me:String classLocation = MyClass.class.getName().replace('.', '/') + ".class";ClassLoader loader = MyClass.class.getClassLoader();And then adding:try{URL location = loader.getResource(classLocation);}...to get the actal URL location of the class.Thanks for the short reply!!
Icarin
A: 

Dynamic Web Projects generate WAR files.

The server may or may not expand the WAR file back to a file system structure.

You're best off using the Class or ClassLoader .getResourceAsStream("/FileFolder/MyFile.txt") which can read files from JAR/WAR files, and returns an InputStream.

Example:

reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/FileFolder/MyFile.txt")));

Edit: If this is from a Servlet, consider using gawi's answer instead.

Edit 2: If this is in a static method, you'll need to use MyClass.class instead of this.getClass(), where MyClass is the class name.

R. Bemrose
Actually, yes. I found that using a getClassLoader worked for me: String classLocation = MyClass.class.getName().replace('.', '/') + ".class"; ClassLoader loader = MyClass.class.getClassLoader(); And then adding: try{ URL location = loader.getResource(classLocation); }... to get the actal URL location of the class. Thanks for the short reply!!--- Sorry for the double post of this comment. I'm not using a servlet per sé to access the file. It's a standard java class which is called in turn by a servlet but I don't think that's relevant right now.
Icarin