Android Directory Size

I need to calculate the physical size of a directory. A naive algorithm for this could be:

public static long getFolderSize(File dir) { long size = 0; for (File file : dir.listFiles()) { if (file.isFile()) { System.out.println(file.getName() + " " + file.length()); size += file.length(); } else size += getFolderSize(file); } return size; } 

but how to deal with symbolic links?

+6
source share
3 answers

getCanonicalPath () This is usually due to the removal of redundant names such as ".". and ".." from the path name, allowing symbolic links (on UNIX platforms). http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html

+1
source

My solution to the first question is how to calculate the physical size: How can I get the size of a folder on an SD card in Android?

And here is the solution to detect symlinks: Java 1.6 - defining symbolic links

0
source

It is better to use another API to get the size of the API file that is relevant to this would be

 public static long getFolderSize(File dir) { long size = 0; for (File file : dir.listFiles()) { if (file.isFile()) { System.out.println(file.getName() + " " + file.getTotalSpace()); size += file.getTotalSpace(); } else size += getFolderSize(file); } return size; } 
-1
source

Source: https://habr.com/ru/post/902097/


All Articles