18

Possible Duplicate:
Most efficient way to check if a file is empty in Java on Windows

How to a check if a file is empty in Java 7?
I tried it using the available() method from ObjectInputStream, but it returns always zero even if the the file contains data.

Community
  • 1
  • 1
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187

6 Answers6

36
File file = new File("file_path");
System.out.println(file.length());
Uchenna Nwanyanwu
  • 3,174
  • 3
  • 35
  • 59
  • 3
    A tricky solution since Java 7 is to use the java.nio.Files class. Then, you can perform the same in one line. For example: `System.out.println(Files.size(Paths.get("file_path")));` – daniel souza Apr 18 '15 at 22:50
  • 1
    Doesn't for for Unicode files (see answer in duplicate message for details). – Kalle Richter Jan 08 '17 at 15:12
19
File file = new File(path);

boolean empty = !file.exists() || file.length() == 0;

which can shortened to:

boolean empty = file.length() == 0;

since according to documentation the method returns

The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Hey @Jack, I think it should rather be as my answer below. Correct me if I am wrong. – TechSpellBound Apr 23 '12 at 16:58
  • 1
    It depends upon why you need to check if a file it's empty. You could need either to consider a non existent file as empty or not. So your answer could be as correct as mine, not rather. – Jack Apr 23 '12 at 17:13
  • But I feel that even in that case, if the file does not exist, we don't have to check if its empty or not. So, for a non-existing file, my answer saves a call to file.length(). Minor improvement in efficiency but I feel it's logical. Please correct me if I am wrong. – TechSpellBound Apr 23 '12 at 17:20
  • 1
    In your snippet, if file doesn't exists then `empty == false`. In my snippet if file doesn't exist first condition evaluates to true, so the length of the file it's not checked too, but the result is different (remember that this is an `||`, laziness of evaluation applies to both `&&` and `||`operators). – Jack Apr 23 '12 at 18:08
  • 2
    In addition there's no need to check for existence in any case, since `file.length()` returns `0` if file doesn't exist, it's just for a matter of readability. – Jack Apr 23 '12 at 18:10
4
File file = new File(path);

boolean empty = file.exists() && file.length() == 0;

I want to stress that if we want to check if the file is empty, then we have to consider that it exists.

TechSpellBound
  • 2,505
  • 6
  • 25
  • 36
  • I think this one is the correct answer. If you check the javadocs of the length method it says that it will return 0 if the file is "invalid". – Lakatos Gyula Aug 02 '18 at 18:26
2
BufferedReader br = new BufferedReader(new FileReader("your_location"));     
if (br.readLine()) == null ) {
    System.out.println("No errors, and file empty");
}

see Most efficient way to check if a file is empty in Java on Windows

Community
  • 1
  • 1
Oritm
  • 2,113
  • 2
  • 25
  • 40
  • @sans481 the linked post pretty much enumerates every method known to man. Most of those methods generally work and are correct, just the OP there had a very specific problem: a file that was actually **not empty** (had a two-byte BOM in it) was to be considered as empty. – Marko Topolnik Apr 23 '12 at 13:34
  • Now I clearly understood your opinion. Yes, its really a disaster to read a whole line just to tell whether or not a file is empty. Situation becomes worse if the first line is huge. Thanks! – sgowd Apr 23 '12 at 13:40
1

According to J2RE javadocs: http://docs.oracle.com/javase/7/docs/api/java/io/File.html#length()

public long length()
    Returns the length of the file denoted by this abstract pathname. The return value is     unspecified if this pathname denotes a directory.

So new File("path to your file").length() > 0 should do the trick. Sorry for bd previous answer. :(

Nicocube
  • 2,962
  • 2
  • 20
  • 28
0
        File file = new File("path.txt");
        if (file.exists()) {
            FileReader fr = new FileReader(file);
            if (fr.read() == -1) {
                System.out.println("EMPTY");
            } else {
                System.out.println("NOT EMPTY");
            }
        } else {
            System.out.println("DOES NOT EXISTS");
        }
rubenGL
  • 89
  • 1
  • 2
  • 9