• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to Calculate folder size  RSS feed

 
saikiran venkata
Greenhorn
Posts: 29
Hibernate Java
  • Mark post as helpful
  • send pies
  • 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...,
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • 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: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saikiran kiran wrote:. . . it loops through it . . .
[Pedantic mode]It doesn't loop. It recurses...[/Pedantic mode]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried it on my windows machine with D:\\

Got the size of the drive
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!