user1144342
user1144342

Reputation: 21

Getting file attributes in Java

Given a java.io.File that represents a .exe file, how can I discover the file attributes - for example: size and date created, and how could I discover the data in the executable properties - for example: file version?

Upvotes: 2

Views: 23782

Answers (5)

Tom Mendes Pereira
Tom Mendes Pereira

Reputation: 21

Simple example:

You'll need some imports...

    import java.nio.file.Path;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.nio.file.attribute.BasicFileAttributes;

    String fileRoot = "";
    File file = new File(fileRoot);
    if (file.isFile()) {
        Path path = Paths.get(fileRoot);
        BasicFileAttributes attr;
        try {
            attr = Files.readAttributes(path, BasicFileAttributes.class);
            System.out.println("creationTime     = " + attr.creationTime());
            System.out.println("lastAccessTime   = " + attr.lastAccessTime());
            System.out.println("lastModifiedTime = " + attr.lastModifiedTime());

            System.out.println("isDirectory      = " + attr.isDirectory());
            System.out.println("isOther          = " + attr.isOther());
            System.out.println("isRegularFile    = " + attr.isRegularFile());
            System.out.println("isSymbolicLink   = " + attr.isSymbolicLink());
            System.out.println("size             = " + attr.size());
        } catch (IOException ex) {
            Logger.getLogger(MGSiap.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Upvotes: 0

Chuck Adams
Chuck Adams

Reputation: 777

Presumably you're referring to NTFS Extended Attributes here, and it's only recently (as of Java SE 7) that you've been able to get at these without some kind of windows specific JNI hackery. This should be doable with the new NIO2 file attributes interface. Some of these attributes, such as size, are basic attributes, while others such as version are "user" attributes.

Probably the best page with examples that explains the use of this API is at http://www.ibm.com/developerworks/java/library/j-nio2-2/ -- search down for "File attributes" if you're not interested in the examples of the other APIs.

Upvotes: 9

Aravind Yarram
Aravind Yarram

Reputation: 80186

Look at the java.io.File class. It has methods like canExecute(), length().

Creation date: Read this post.

Efficient way to get the file size: java get file size efficiently

Upvotes: 0

user130076
user130076

Reputation:

If you want the version of an executable file then that is almost impossible to do in the general case as there is no standard way of versioning the files (within that file format).

The other information should be easy to get. Checkout this answer: How to discover a File's creation time with Java?

Upvotes: 0

JuanZe
JuanZe

Reputation: 8157

Just calling several File class getters... Check the javadocs for the File class and look at this tutorial: http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

Upvotes: 1

Related Questions