4

I'm trying to get a list of resources from classpath directory. I'm using JDK 1.7 on Mac OS X. I'm using response to this question to solve my problem. I can open and read a single file, however when I try to read whole directory I get empty list of files.

This works:

InputStream is = loadFile("scenes/test.xml");
InputStreamReader sReader = new InputStreamReader(is);
BufferedReader bReader = new BufferedReader(sReader);
System.out.println(bReader.readLine()); // print "<Empty/>"

loadFile method looks that way:

private InputStream loadFile(String filePath) throws IOException {
    InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
    if (inStream == null) {
        throw new IOException("Unable to load file from resources " + filePath);
    }
    return inStream;
}

and this doesn't

InputStream is = loadFile("scenes/");
InputStreamReader sReader = new InputStreamReader(is);
BufferedReader bReader = new BufferedReader(sReader);
System.out.println(bReader.readLine()); // print "null"

Anyone can help with this problem?

Community
  • 1
  • 1
pstrag
  • 617
  • 5
  • 16

1 Answers1

1

You're not accessing the files. You just access the directory. You'll need to loop through each file in the path.

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
Zanidd
  • 133
  • 13