Help with listFiles()
I'm trying to read all the filenames of a specific web directory for an online photogallery. The reading is done using a custom jsp tag, which creates a js variable with a list of all the picture names. My code works fine when reading from my own PC. For example reading all the names from "C:\Program Files" works perfect. The problem is that I get an error when I try to read from a web directory. The error is a null pointer error that appears at the start of my for loop. Which I'm assuming is because the listFiles() method isn't returning anything. Even "http://localhost:8080/test" gives me an error (since I thought it might have something to do with trying to access a directory outside of my domain.) Can anyone help me with the proper way to read a web directory? My code is below:
if(name !=null){
StringTokenizer toker;
File f1 =new File(name);
File[] list = f1.listFiles();//Get all files in the directory "name"
String files ="", fname;
for(int i=0;i<list.length;i++){
fname=list[i].toString();//Turn filename into a string
toker =new StringTokenizer(fname,"\\"); //parse out the path before the filename
while(toker.hasMoreTokens())
fname=toker.nextToken();
files = files +"\"" + fname.substring(0, fname.length()-4) +"\",";//parse out the extension ".jpg"
}
files = files.substring(0,files.length()-1);
pc.getOut().write("><script type=\"text/javascript\"> \n var dirlist = new Array("+files+"); \n </script>");//send the string of filenames to the client
}
[2212 byte] By [
ner0a] at [2007-11-27 4:08:24]

fname=list[i].toString(); //Turn filename into a string
Does this do what you think it does? What if list[i]
is another directory?
However, your question
File[] list = f1.listFiles(); //Get all files in the directory "name"
String files = "", fname;
if (list != null)
{for(int i=0;i<list.length;i++){
fname=list[i].toString(); //Turn filename into a string
toker = new StringTokenizer(fname,"\\"); //parse out the path before the filename
while(toker.hasMoreTokens())
fname=toker.nextToken();
files = files + "\"" + fname.substring(0, fname.length()-4) + "\","; //parse out the extension ".jpg"
}
Message was edited by:
xiarcel>
Are you asserting that "http://localhost:8080/test" is the name of a directory? It may be the case that your web server is configured to allow access to any file in that directory, and it may also be the case that the URL itself will return some kind of directory in some kind of HTML format.
But you certainly can't use a java.io.File object to get that directory. An HTTP URL is not a File.
However since you're doing this on the server side anyway, I don't see why you need remote access to the directory. It's on the server already, isn't it?
Yes, "http://localhost:8080/test" is an online directory. It's not a file. It's a folder that contains a bunch of files. I can hard code all the file names into my servlet, but then I'd have to change my servlet every time I add a new file to that directory, which I don't want to have to do. I also want to avoid passing a large array, containing all the file names, as a parameter to the servlet.
ner0a at 2007-7-12 9:13:45 >

Your understanding of what http://localhost:8080/test means is flawed. ALL http://localhost:8080/test is, is a URL. What happens when you access that URL is up to the server. Now it could be that the server is configured to show a bunch of text that correspond to a list of files present in some directory on the server. However, that does NOT make it behave in the same way as a file or folder that can be accessed using the File class.
What happens when you go to http://localhost:8080/test in a browser? What do you think you see there? You may think you see the contents of a folder, but what you actually see is an HTML page that contains text that looks like the contents of a folder. Do you understand the difference?
It's like a picture of an apple. When you point people to a picture of an apple and ask "What's that?", they will often respond: "It's an apple". Which of course is wrong. It's a picture of an apple.