开发者

Go to a specific directory in Java Servlet

i have a app folder in tomcat webapps named BankApp, and there is a temp directory in it.

I want to get all the files in temp folder

I tried this :

File file = new File(path + "/temp/");

File开发者_运维百科[] list = file.listFiles();

for(int i=0;i<list.length;i++)    
{
    out.println(list[i].getName() + "<br/>");
}

but its not working, i mean giving null error

is it the right way to go to that folder ? i.e. BankApp\temp

Thanks !


Not a final answer, from the JavaDoc for java.util.File.listFiles():

An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

Are you sure the statement path + "/temp/" returns a valid (existing) path?

Edit: You must correctly identify the root path of your BankApp. You can do this by calling ServletContext.getRealPath("/"). See my answer to a previous question.


Define "it's not working" -- what happens?

If you want the files in the temp directory why do you getParentFile and list the files in that directory instead?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜