13

I am trying to read a text file from my war archive and display the contents in a facelets page at runtime. My folder structure is as follows

+war archive > +resources > +email > +file.txt

I try to read the file in the resources/email/file.txt folder using the following code

File file = new File("/resources/email/file.txt");
BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
StringBuffer buffer = new StringBuffer();
if (reader != null) {
    String line = reader.readLine();
    while (line != null) {
        buffer.append(line);
        line = reader.readLine();
// other lines of code

The problem however is that when I the method with the above code runs, A FileNotFoundException is thrown. I have also tried using the following line of code to get the file, but has not been successful

File file = new File(FacesContext.getCurrentInstance()
        .getExternalContext().getRequestContextPath() + "/resources/email/file.txt");

I still get the FileNotFoundException. How is this caused and how can I solve it?

Community
  • 1
  • 1
Paul Plato
  • 1,471
  • 6
  • 28
  • 36

4 Answers4

27

Try below:

   InputStream inputStream = 
      getClass().getClassLoader().getResourceAsStream("/resources/email/file.txt");
   BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream ));
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
10

Try to avoid the File, as this is for reading things from the file system.

As your resource is bundled into the WAR, you can access it via the classloader.

Ensure that the resource is bundled into your WEB-INF/classes folder.

InputStream in =
new InputStreamReader(FileLoader.class.getClassLoader().getResourceAsStream("/resources/email/file.txt") );

This is a good blog on the topic

http://haveacafe.wordpress.com/2008/10/19/how-to-read-a-file-from-jar-and-war-files-java-and-webapp-archive/

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
Tinman
  • 786
  • 6
  • 18
  • 1
    Thanks. One question a day is all it takes. – Tinman Nov 06 '12 at 01:23
  • I have tried with the above example, i have the file listed in the following directory structure /WEBINF/classes/resources/file.txt. However I still get the following exception Caused by: java.lang.NullPointerException at java.io.Reader.(Unknown Source) [rt.jar:1.7.0_04] at java.io.InputStreamReader.(Unknown Source) [rt.jar:1.7.0_04] at org.xanosms.emailsender.RegistrationEmailLoader.loadFile(RegistrationEmailLoader.java:43) [classes:] – Paul Plato Nov 06 '12 at 08:10
  • 1
    Sorry, i found that omiiting the .txt extension solves the problem. Thank you very much for the help. I appreaciate!! – Paul Plato Nov 06 '12 at 08:34
4

If you want to get the java File object, you can try this:

String path = Thread.currentThread().getContextClassLoader().getResource("language/file.xml").getPath();
File f = new File(path);
System.out.println(f.getAbsolutePath());
lidox
  • 1,901
  • 3
  • 21
  • 40
1

I prefer this approach:

InputStream inputStream = getClass().getResourceAsStream("/resources/email/file.txt");

if (inputStream != null) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
       ...
    } catch ...
} else ...

Three reasons:

  • it supports both: loading resources from an absolute path and from a relative path (starting from the given class) -- see also this answer
  • the way to obtain the stream is one step shorter
  • it utilizes the try-with-resources statement to implicitly close the underlying input stream
Community
  • 1
  • 1
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106