开发者

Go to a specific directory in Java Servlet

开发者 https://www.devze.com 2023-04-09 12:50 出处:网络
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 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

精彩评论

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

关注公众号