EN
Java - get file information
0
points
In this article, we would like to show you how to get file information in Java.
There are several methods to get file information in Java.io.File package:
getName()
- returns the name of the given file object,getAbsolutePath()
- returns the absolute pathname of the given file object,canWrite()
- returns a boolean value representing whether the application is allowed to write the file or not,canRead()
- returns a boolean value representing whether the application is allowed to read the file or not,getParent()
- returns a String value which is the path of the Parent of the given file object,length()
- returns the length of the file in bits.
Practical example
import java.io.File;
public class Example {
public static void main(String[] args) {
File file = new File("C:\\projects\\dirask.txt");
if (file.exists()) {
System.out.println("file name: " + file.getName());
System.out.println("absolute path: " + file.getAbsolutePath());
System.out.println("writeable: " + file.canWrite());
System.out.println("readable " + file.canRead());
System.out.println("parent path: " + file.getParent());
System.out.println("file size in bytes " + file.length());
} else {
System.out.println("File doesn't exist.");
}
}
}