In this example we will show the methods to get the properties of a file in Java.
Source Code
package com.beginner.examples;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;
public class FileExample {
public static void main(String[] args) {
File file = new File("example.txt");
long time = file.lastModified();
Date date = new Date(time);
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String lastTime = dateFormat.format(date);
System.out.println("parent:"+file.getParent());
System.out.println("name:"+file.getName());
System.out.println("absolute Path:"+file.getAbsolutePath());
System.out.println("path:"+file.getPath());
System.out.println("length:"+file.length());
System.out.println("lastModified time:"+lastTime);
}
}
Output:
name:example.txt
absolute Path:D:Workspacescasesexample.txt
path:example.txt
length:10
lastModified time:2019-02-25 15:04:49
References
Imported packages in Java documentation: