Re: how to detect a folder size
Posted By:
skyyoung_shmilu
Posted On:
Sunday, October 7, 2001 09:06 PM
import java.net.*;
import java.io.*;
import java.util.*;
/**
* Shows the size of a file system directory/folder.
*/
public class FolderSize
{
public static void main(String args[]) throws Exception
{
if (args.length < 1)
{
System.err.println("usage: java FolderSize StartFolderName");
System.exit(1);
}
System.out.println("Size = " + getFolderSize(new File(args[0])));
}
public static long getFolderSize(File folder)
throws IllegalArgumentException
{
// Validate
if (folder == null ¦¦ !folder.isDirectory())
throw new IllegalArgumentException("Invalid folder");
String list[] = folder.list();
if (list == null ¦¦ list.length < 1)
return 0;
// Get size
File object = null;
long folderSize = 0;
for (int i=0; i < list.length; i++)
{
object = new File(folder, list[i]);
if (object.isDirectory())
folderSize += getFolderSize(object);
else
if (object.isFile())
folderSize += object.length();
}
return folderSize;
}
}
Re: how to detect a folder size
Posted By:
Dermot_Hennessy
Posted On:
Monday, July 30, 2001 02:58 AM
Does java.io.File.length() not work then?
Dermot