storing of file information(such as size,modified date,name...)

J

James

what is the suitable data type to store file information?
(arraylist?,vector?array?.....)
when using java.io.File, I can get the file date,file size, file name, and
whether is folder or is file, so I want to store this information as
variable for processing such like comparing this file name,date,size with
other. So how to do this?
Because of when I "listfile", inside there also got file and folder, I need
to specific it out, so it may got somekind of nested inside the data.
If I want to know what is the size of that folder, I need to total up all
the file that inside that folder, if still found other folder, I need to go
in and add more.

Please show some example of code.
 
P

Phil...

For an individual file or directory, why not use a "File" object?
It contains everything you need.
For a list of files that are the contents of a directory
you can use an array of "Files" which can be easily
populated by using code like this
File files[] = dir.listFiles();
(where "dir" is a "File" that represents a directory)
Note that if the directory contains any subdirecties they will be included
in "files[]"

HTH, phil...
 
C

Chris Chilvers

Because of when I "listfile", inside there also got file and folder, I
need
to specific it out, so it may got somekind of nested inside the data.
If I want to know what is the size of that folder, I need to total up all
the file that inside that folder, if still found other folder, I need to go
in and add more.

Please show some example of code.

An easy way to solve that is a recursive function.

/**
* Returns the size of a file or directory. If it is a directory
* it will recurse through all sub directories and file to calculate
* it's total size
* @param f the file/directory to get the size of
* @return The total size of the file/directory in bytes
*/
long getSize(File f) {
//get the files in the directory
File inDir[] = f.litFiles();

//if inDir is null then it was a file
//so just return it's size
if (inDir == null)
return f.getSize();

//go through each file calling this function to get it's size
//if it is a file it will only get as far as the above line and
//return it's size. If it is a directory it will run this loop
//to get the size of each file in that directory as well.
int size = 0;
for (int i = inDir.length; i-- > 0; )
size += getSize(inDir);
return size;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top