This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on the method is file

 
Sailing
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic