How to know file and directory size in java without creating an object?

First, please do not miss, because you might think that this is a general question, it is not. I know how to find out the file and directory size using file.lengthand ApacheFileUtils.sizeOfDirectory .

My problem is that the files and directory size are too large (in hundreds of mb). When I try to figure out the size using the code above (for example, creating a file object), my program becomes so resource intensive and slows down performance.

Is there a way to find out the file size without creating an object?

I use for files File file1 = new file (filename); long size = file1.length ();

and for the directory, File dir1 = new file (dirPath); long size = fileUtils.sizeOfDirectiry (dir1);

I have one parameter that allows me to calculate the size. If the parameter is false, then it goes smoothly. If false, the program slows down or freezes. I calculate the size of 4 directories and 2 database files.

+3
source share
6 answers

Answering my own question ..

This is not the best solution, but works in my case.

I created a script package to get the size of the directory and then read it in a java program. This gives me less runtime when the number of files in the directory is greater than 1L (this is always in my case). SizeOfDirectory takes about 30255 ms and with the script package I get 1700 ms .. For less files, the script package is expensive.

+1
source

. - , , HD, . (, ), , ( ).

+4

- . , .

, , , . 10 ( ). , 100 000 , 17 , .

- . 0,1 , 10 , 100 . .

BTW: , . , .


EDIT: , . , , .

$ time du -s /usr
2911000 /usr

real    0m33.532s
user    0m0.880s
sys 0m5.190s

$ time du -s /usr
2911000 /usr

real    0m1.181s
user    0m0.300s
sys 0m0.840s

$ find /usr | wc -l
259934

, , , . , , .

Timing FileUtils.sizeOfDirectory( "/usr" ) 8,7 . , , 30 . .

Runtime.exec("du -s "+directory);, . , , , .

+3

File.listFiles() .

10 , 10 000 . , , .

FileFilter, , .

, 70% File.isDirectory( , Apache ). isDirectory ( ).

File.isDirectory , .

, , isDirectory .

listFiles , , , , File.

, , , Java (, ).

+2

, , , (, sub dirs) - , file.listFiles(), ( , ). , , - , fileUtils , , .

- , - , .

0

All Articles