爲什麼通配符在下面的java代碼中不起作用?
我的要求看起來像http://localhost:8080/App/DataAccess?location=Dublin
使用通配符列出與java目錄中的文件時
[email protected]:~$ 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代碼(DataAccess.java)。
(DataAccess.java:27)
指for循環..
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File[] files = finder("/usr/local/CustomAppResults/" +
request.getParameter("location") + "/*/");
for (int i = 0; i < 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");
}
});
}
錯誤:
The server encountered an internal error that prevented it
from fulfilling this 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)
[如何找到與Java中的通配符字符串匹配的文件?](http://stackoverflow.com/questions/794381/how-to-find-files-that-match-a-wildcard-string -in-java) –
[Java教程](http://docs.oracle.com/javase/tutorial/essential/io/find.html)涵蓋java.nio中的glob匹配 – Romski