While googling, I see that using java.io.File#length() can be slow. FileChannel has a size() method that is available as well.
Is there an efficient way in java to get the file size?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, I tried to measure it up with the code below:
For runs = 1 and iterations = 1 the URL method is fastest most times followed by channel. I run this with some pause fresh about 10 times. So for one time access, using the URL is the fastest way I can think of:
For runs = 5 and iterations = 50 the picture draws different.
File must be caching the calls to the filesystem, while channels and URL have some overhead.
Code: