• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading filename in war archive  RSS feed

 
Andre Fean
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a servlet in a war archive I'm trying to read all the filenames which are contained in a directory. The only information I have is the name of the directory.
Is this possible or does the classloader avoid this?
thanx & cheers,
andre
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet runs as an application on the host machine, so there's practically no limit as to what it can do. Your question is vague as to where the directory is, on disk or within the WAR file. If the directory is on disk you could create a File object for the current directory and use its getParent() and list() methods to perform a brute-force search.
If you know a file name within the directory you can use Class.getResource() to locate the file (as long as it's in the CLASSPATH). That method returns a URL instance, but File takes a URI argument, so you have do the following: new File(new URI(url.toString())). Invoke getParent() on that File instance and you have a File representing the directory.
If you want to look within the WAR file, you can use Class.getResourceAsStream() to get an InputStream representation of the WAR, then use java.util.zip.ZipInputStream to browse the file contents. I think. YMMV
 
Andre Fean
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I have a directory under the WEB-INF dir (in the war archive) with some property files. But I don't know how many files will be there and what the names of these files will be.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!