File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to print list of Files?  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to print list of Files?  " Watch "How to print list of Files?  " New topic
Author

How to print list of Files?

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
I have a list of Files. I would like to print their names. I tried as below:
But I get an exception as below: Can anyone tel me what the problem is?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41511
    
  53
You can't cast a File to a String. Something like "System.out.println(((File) it.next).getName())" should do the trick.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Probably better however to use a parametrised Iterator.

. . .
Iterator<File> it = javaFiles.iterator();
. . .
System.out.println(it.next().getName());

That way you can dispense with the class casting.
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks for the tricky answer. I am able to print the name of the file.
But I have to store the file name in a string then start manupulating that. How do I do that? Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41511
    
  53
"((File) it.next).getName()" is the file name as a string. What else are you looking for?
S Keith
Greenhorn

Joined: Feb 06, 2008
Posts: 4
Heres some code that should explain things.

I've tried to keep it similar to the code you gave.



running this gives:




http://java.sun.com/javase/6/docs/api/java/lang/String.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to print list of Files?