Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking empty file

 
Nikes Shah
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
I do not try this code , but i think , it works, answer when you tried.
[ September 24, 2008: Message edited by: Anton Pechsherov ]
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic