This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I am a beginner of java. I have written a code for list all the file in the direction.
But I found some problem on running this class. If the class is at the same dir as the path parameter I call this class. The result will be fine. But if I the path parameter is a different path with which the class located. Only the .class, .java will be return as isFile = true. The other file will displayed as directory. Do I made any mistake in the code above. Thanks
[This message has been edited by Sailing (edited June 27, 2001).] (edited by Cindy to format code) [This message has been edited by Cindy Glass (edited June 27, 2001).]
Mikael Jonasson
Ranch Hand
Joined: May 16, 2001
Posts: 158
posted
0
Is the code a cut from your program, or did you type it? The reason I ask is that the for-loop looks kind of strange. It's missing the end perantesis for one thing. /Mike
Sailing
Greenhorn
Joined: Jun 26, 1999
Posts: 3
posted
0
Originally posted by Mikael Jonasson: Is the code a cut from your program, or did you type it? The reason I ask is that the for-loop looks kind of strange. It's missing the end perantesis for one thing. /Mike
I changed that. please check it again
Mikael Jonasson
Ranch Hand
Joined: May 16, 2001
Posts: 158
posted
0
Have you tried including the original directory in the the files created in the loop? File f = new File(args[0],files[i]); Not sure it will help, but it's a suggestion. /Mike
Sailing
Greenhorn
Joined: Jun 26, 1999
Posts: 3
posted
0
Originally posted by Mikael Jonasson: Have you tried including the original directory in the the files created in the loop? File f = new File(args[0],files[i]); Not sure it will help, but it's a suggestion. /Mike
Thanks Mike, I try to change that to File f = new File(args[0]+ files[i]); but still not work. Your suggestion is correct. It's really appreciated for your help
rani bedi
Ranch Hand
Joined: Feb 06, 2001
Posts: 358
posted
0
http://java.sun.com/j2se/1.3/docs/api/index.html According to the API the constructor File(String pathname)- Creates a new File instance by converting the given pathname string into an abstract pathname. list() method returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. listFiles() method returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. In your program (the lines i have commented) you were creating a file object by passing the filename in the constructor. Whereas it expects the file pathname. Bye changing the function from list() to listFiles() program will give the required output.
Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Sean MacLean
author
Ranch Hand
Joined: Nov 07, 2000
Posts: 621
posted
0
"Sailing", The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements. Thanks. Sean