aspose file tools*
The moose likes Java in General and the fly likes Folder and file attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Folder and file attributes" Watch "Folder and file attributes" New topic
Author

Folder and file attributes

Imre Tokai
Ranch Hand

Joined: Jun 04, 2008
Posts: 130
Hello,


How do I get folder attributes, original date created, folder size and # of records in folder?
For files, I'd want original date created?

What is the simplest approach? I'm checking http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html now


Regards
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

"Date created" isn't one of the attributes you can get with Java.

The "size" of a folder is always zero, as you will see by calling the appropriate method from the File class. (The documentation says it's unspecified, but in all the environments I have tried it in, you get zero.) If you want to know the sum of the sizes of all the files in a folder, you can write code to compute that.

I don't understand what you mean by "# of records in a folder". If you want a list of the files in a folder, there's a method in the File class which returns such a list.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

Actually an array

To calculate the size of a folder you'll need a recursive call. In summary, the size of a folder is the sum of the sizes of all files in the folder, plus the sum of the sizes of all sub folders. The size of those sub folders is the recursive call.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Imre Tokai
Ranch Hand

Joined: Jun 04, 2008
Posts: 130
Thank you for your answers!


Remaining issue: Date Created
I'm able to reach: http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#lastModified()
but not the date created;
However, when I right-click on folder from Windows Explorer I'm able to read "Created:" date. How to get this date via Java?

For folder size i recommend this method:

For records in folder i recommend: http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#list()

How to get the Date Created for a folder?


Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

As Paul said, you can't get this with pure Java. You can use JNI, or perhaps call an external application (although I can't name one for you), but those are your options.
Gopi Chella
Ranch Hand

Joined: Apr 26, 2010
Posts: 53
Hello,

Use the below method to get the folder created date.



SCJP 1.5
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

The last modified date is not the create date. The create date is set only once, when the file is created. The last modified date is updated each time the file is, well, modified. Hence the name.
Gopi Chella
Ranch Hand

Joined: Apr 26, 2010
Posts: 53
Thanks Rob !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Folder and file attributes
 
Similar Threads
Find the path in ant
creating same file twice - size differs
Drag and drop with JTreeTable
Watchdog process to watch a folder
Two jboss instances with different JVM