jQuery in Action, 3rd edition
The moose likes I/O and Streams and the fly likes Checking empty file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Checking empty file" Watch "Checking empty file" New topic

Checking empty file

Nikes Shah
Ranch Hand

Joined: Jul 18, 2007
Posts: 133
Hi Friends,

1. I want to upload various file formats like TXT,CSV, .XLS in our web application from browser.

But before that I want check whether file is empty or not. Can anyone suggest best practice to check file is empty or not.

2. When I use length() function for File object it provides required result. But when I use "%" in file name then it gives message file is empty even if file has some contents in it.

Thanks for help in advance.
Anton Pechsherov

Joined: Sep 09, 2008
Posts: 27
I do not try this code , but i think , it works, answer when you tried.
[ September 24, 2008: Message edited by: Anton Pechsherov ]

I make different errors when writing posts...because i don't speak English...
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

I strongly advise against using available() - it can return 0 because the stream is blocked, even though there are still enough bytes to read. It probably won't occur with Files but the API does not guarantee it won't.

The same approach can be used with read() though:

Because read() is guaranteed to return -1 when the end of the stream has been reached, if the very first read() returns -1 the file must be empty.

Don't forget to close your Readers, Writers and Streams!
[ September 25, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: Checking empty file
jQuery in Action, 3rd edition