Thursday, 29 August 2013

How to find the size of the largest file in a directory?

How to find the size of the largest file in a directory?

I need to find out the size of the largest file/folder in a directory. I
have done it in the below way.
private static Long getSizeofLargestFile(String theRootFolder)
{
Long aLargestFileSize = 0L;
File aRootDir = new File(theRootFolder);
for (File aFile : aRootDir.listFiles())
{
if (aLargestFileSize < aFile.length())
{
aLargestFileSize = aFile.length();
}
}
return aLargestFileSize / (1024 * 1024);
}
Can there be a better way than this?

No comments:

Post a Comment