21

It is possible to know the metadata of a file in java? and if it is, How to get the metadata of a file in java?

2
  • I think this is platform-dependent. Am I right in saying you're after the data the file system "attaches" to a file?
    – user377628
    May 30, 2012 at 20:52
  • 1
    If that's what I'm looking for.
    – josemm1790
    May 30, 2012 at 20:54

5 Answers 5

42

There is a basic set of metadata that you can get from a file.

Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, 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());

Some things are platform dependent and may throw exceptions or return unexpected results.

You can read more at Managing Metadata (File and File Store Attributes).

6

FITS is a command line app that bundles many tools that can identify and characterize files (extract metadata). It also has a java API

Also there are numerous identification and characterization tools that can do similar tasks. Apache Tika, Pronom Droid, Jhove, etc.

0
3

Get file Metadata from java program

package demo.test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;

public class FileCreationTime {
public  String getCreationDetails(File file)
    {       
       try{         
        Path p = Paths.get(file.getAbsolutePath());
        BasicFileAttributes view
           = Files.getFileAttributeView(p, BasicFileAttributeView.class)
                  .readAttributes();
        FileTime fileTime=view.creationTime();
        return (""+new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format((fileTime.toMillis())));
       }
       catch(IOException e){ 
        e.printStackTrace(); 
       }
       return ""; 
   }

   public static void main(String...str){
       System.out.println
           (new FileCreationTime().getCreationDetails(new File("D:/connect.png")));
   }
}
0

I think is easy to use this folowing project to get informations from any files.

import java.io.File;

public class FileInfo{
    public static void main (String []args){
        File f = new File("file100.txt");
        if(f.exists()){
            System.out.println("Name: "+ f.getName());
            System.out.println("Path: "+ f.getAbsolutePath());
            System.out.println("Size: "+ f.length());
            System.out.println("Writeable: "+ f.canWrite());
            System.out.println("Readable: "+ f.canRead());
        }else{
            System.out.println("The File does not exist");
        }
    }
}
-1

With Java 7 you have nio2 package, with new Path.class giving all you are looking for

1
  • You should see How to Answer about links in an answer. I didn't downvoted because this links is most likely there to last (but Oracle can still move them...). You should add example in your answer or at least give some methods.
    – AxelH
    Nov 20, 2017 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.