Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See if file is empty [duplicate]

Tags:

java

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.

like image 396
Ramy Al Zuhouri Avatar asked Apr 23 '12 13:04

Ramy Al Zuhouri


People also ask

How do you check the file is empty or not?

You can use the find command and other options as follows. The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data.

How check if file is empty C++?

#include<iostream> #include<fstream> using namespace std; int main() { ifstream read("test. txt"); if(! read) return 0; bool isEmpty = read. peek() == EOF; cout << boolalpha << "test …


2 Answers

File file = new File("file_path");
System.out.println(file.length());
like image 189
Uchenna Nwanyanwu Avatar answered Oct 05 '22 02:10

Uchenna Nwanyanwu


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

like image 22
Jack Avatar answered Oct 05 '22 02:10

Jack