Using wildcard when listing files in directory with java -
why wildcard not work in java code below?
request looks http://localhost:8080/app/dataaccess?location=dublin
rob@work:~$ ls /usr/local/customappresults/dublin/*/.history /usr/local/customappresults/dublin/team1/.history /usr/local/customappresults/dublin/team2/.history /usr/local/customappresults/dublin/team3/.history
servlet code (dataaccess.java).
(dataaccess.java:27)
refers loop ..
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { file[] files = finder("/usr/local/customappresults/" + request.getparameter("location") + "/*/"); (int = 0; < files.length; i++){ system.out.println(files[i].getname()); } } private file[] finder(string dirname) { file dir = new file(dirname); return dir.listfiles(new filenamefilter() { public boolean accept(file dir, string filename) { return filename.endswith(".history"); } }); }
error:
the server encountered internal error prevented fulfilling request. java.lang.nullpointerexception com.example.servlets.dataaccess.doget(dataaccess.java:27) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:728)
the method public file[] listfiles(filenamefilter filter)
returns null if abstract pathname not denote directory, or if i/o error occurs.
(http://docs.oracle.com/javase/7/docs/api/java/io/file.html)
so, why situation? trying use wildcard char (*
) evaluated shell, won't evaluated in new file(path)
. new file(path)
constructor works exact paths.
things directoryscanner
(apache ant) or fileutils
(apache commons-io) solve problem. see comments above further details on possible solutions, including java 7 nio approach (files.newdirectorystream( path, glob-pattern )
).
Comments
Post a Comment