Directory Size in java code example
How to get Directory Size in java code example?
public static long getDirectorySize(String directoryPath)
{
dirSize = 0L;
dirSize = calDirectorySize(directoryPath);
return dirSize;
}
private static long calDirectorySize(String directoryPath)
{
File fd = new File(directoryPath);
if(!fd.isFile())
{
String list[] = fd.list();
for(int i = 0; i < list.length; i++)
{
String tempDirPath = directoryPath + fileDelimiter + list[i];
File ftd = new File(tempDirPath);
if(ftd.isFile())
dirSize += ftd.length();
if(ftd.isDirectory())
calDirectorySize(tempDirPath);
}
}
return dirSize;
}
public static long getDirectorySize(String directoryPath)
{
dirSize = 0L;
dirSize = calDirectorySize(directoryPath);
return dirSize;
}
private static long calDirectorySize(String directoryPath)
{
File fd = new File(directoryPath);
if(!fd.isFile())
{
String list[] = fd.list();
for(int i = 0; i < list.length; i++)
{
String tempDirPath = directoryPath + fileDelimiter + list[i];
File ftd = new File(tempDirPath);
if(ftd.isFile())
dirSize += ftd.length();
if(ftd.isDirectory())
calDirectorySize(tempDirPath);
}
}
return dirSize;
}
Comments
Post a Comment