Languages
[Edit]
EN

Java - get file information

0 points
Created by:
Alicia-Lambert
580

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.");
        }
    }
}

Related posts

Alternative titles

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - file operations

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join