How to check if a file is empty or not using Java

 Posted On  | Yashwant Chavan 

In this tutorial, You will learn how to check if a file is empty or not using Java. File.length() method returns the length of the file. If file is empty it returns the 0 value.

Refer below steps to to create empty file using java.

  • Create new file instance using java.io.File(String path)
  • File.length() method returns the length of the file. If file is empty it returns the 0 value.

Check empty file?

package com.technicalkeeda.app;

import java.io.File;

public class CheckEmptyFile {
    public static void main(String[] args) {

        File file = new File("c://hello.txt");
        if (file.length() == 0)
            System.out.println("File is empty!!!");
        else
            System.out.println("File is not empty!!!");

    }
}

Output

File is empty!!!


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0359 seconds.