how to detect a folder size
3 posts in topic
Flat View  Flat View

Posted By:   Leo_Chan
Posted On:   Monday, July 30, 2001 12:23 AM

Anyone know how to detect a folder size. Since I need to write a program that monitor the file(s) status(read and write) in a specify folder. So I need to the know, how to real time detect the change in size of the folder. I have been made a thread to detect the no. of the file change but I can't detect the file size changed or the folder size size changed.

My program is only needed to run by DOS client and is ok


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.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);
if (object.isFile())
folderSize += object.length();

return folderSize;

Re: how to detect a folder size

Posted By:   Christopher_Schultz  
Posted On:   Monday, July 30, 2001 01:26 PM

You'll probably have to get a list of files in the directory, and add up their sizes. Since the "size" of a directory means different things on different platforms, my guess is that the return value of File.length for directories is useless (like 0 or -1 something).


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?

About | Sitemap | Contact