Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Checking empty file" Watch "Checking empty file" New topic
Author

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
Greenhorn

Joined: Sep 09, 2008
Posts: 27
Try:
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
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking empty file