• 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

Need to Calculate folder size

 
Greenhorn
Posts: 29
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Ranchers,
i wrote a method which takes folder name as argumnet if there are any subfolders it loops through it and gives the total size



it is working fine when i pass the foldername........but it is giving NullPointerException when i pass drive name
eg:
D:\movies ------> working fine

D: -------> not working

Thanks...,
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look through the method you are using, and see under which circumstances it returns null.
Try creating a file object representing your C drive, and see what its properties are, for example whether it counts as a directory. Try System.out.println(new File("C:")); and see whether that gives you the information you require.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saikiran kiran wrote:. . . it loops through it . . .

[Pedantic mode]It doesn't loop. It recurses...[/Pedantic mode]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
listFiles() can return null if a directory could not be read. Under Windows, by default, each drive has a root folder for system restore. This folder cannot be read, and therefore will always cause listFiles to return null. The solution is simple - check if listFiles is null before iterating over it.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried it on my windows machine with D:\\

Got the size of the drive
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
reply
    Bookmark Topic Watch Topic
  • New Topic