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.
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.
#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 …
File file = new File("file_path");
System.out.println(file.length());
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With