• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File.isDirectory()

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



If I comment Line 1, I get the following output:



If I uncomment the same line, I get the following. Instead of printing files in the next level of subdirectory it not even printing the parent level.




Any thoughts?


If I change Line 2 from (File f3 = new File(files[i].getName()) this to File f3 = files[i]; it works.

But I thought it will work even with the file name.
[ May 26, 2007: Message edited by: M Krishnan ]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it too....I got the latter result even when that line is commented...Making the change that you mentioned solves the problem...but I can't figure out why....
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>.I got the latter result even when that line is commented

May be, you don't have any file under that directory. Try creating some files in that directory before trying the listFiles().
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because you are getting exception in your code
and your catch is {}, you have no idea it's there
put there e.printStackTrace() and you'll see error, most likely NullPointerException
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John said it right.
Nothing can be said until you do not handle the exception!

Possibly null is being returned by the method listFiles.

About the peculiar behavior of enhanced for loop like:
Code 1:
11.String s[]=null;
12.for(String ss : s){
13.System.out.println("Simple Stmt!!");
14.}
Throws a null pointer exception at runtime, at line 12.

Code 2:
11.String s[]=null;
12.for(String ss : s){
13.//System.out.println("Simple Stmt!!");
14.}
No exception thrown at runtime.

Can somebody please help in this regard.
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get exception in both.
Possibly you are using some JDK with optimization, which ignores the empty for statement.
 
ramesh vardhan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John.
Yeah, behavior is same when I did not use any IDE.Probably the optimization for an empty for loop is set on my IDE.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic