/**
* <p>
* Created on 2007-5-7 下午11:11:50 <br>
* Copyright (c) 2007, All rights reserved.
* </p>
* @author Widen
* @version: 1.0.0
*/
package com.widen.webstory.test;
import java.io.File;
public class GetFileSize
{
// 递归
public long getFileSize(File f)
throws Exception
{
long size = 0;
File flist[] = f.listFiles();
for (int i = 0; i < flist.length; i++)
{
if (flist[i].isDirectory())
{
size = size + getFileSize(flist[i]);
} else
{
size = size + flist[i].length();
}
}
return size;
}
public static void main(String args[])
{
GetFileSize g = new GetFileSize();
long startTime = System.currentTimeMillis();
try
{
File ff = new File("d:\\application-data");
long l = g.getFileSize(ff);
System.out.println("d:\\application-data 目录的大小为:" + l + "字节...");
} catch (Exception e)
{
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("总共花费时间为:" + (endTime - startTime) + "毫秒...");
}
}
用java获取文件夹的大小
最新推荐文章于 2024-07-13 03:31:11 发布