Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to print list of Files?

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't cast a File to a String. Something like "System.out.println(((File) it.next).getName())" should do the trick.
 
Campbell Ritchie
Sheriff
Posts: 51423
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"((File) it.next).getName()" is the file name as a string. What else are you looking for?
 
S Keith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic