Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 47217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:05:16+00:00 2026-05-10T16:05:16+00:00

While googling, I see that using java.io.File#length() can be slow. FileChannel has a size()

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:05:17+00:00Added an answer on May 10, 2026 at 4:05 pm

    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:

    LENGTH sum: 10626, per Iteration: 10626.0  CHANNEL sum: 5535, per Iteration: 5535.0  URL sum: 660, per Iteration: 660.0 

    For runs = 5 and iterations = 50 the picture draws different.

    LENGTH sum: 39496, per Iteration: 157.984  CHANNEL sum: 74261, per Iteration: 297.044  URL sum: 95534, per Iteration: 382.136 

    File must be caching the calls to the filesystem, while channels and URL have some overhead.

    Code:

    import java.io.*; import java.net.*; import java.util.*;  public enum FileSizeBench {      LENGTH {         @Override         public long getResult() throws Exception {             File me = new File(FileSizeBench.class.getResource(                     'FileSizeBench.class').getFile());             return me.length();         }     },     CHANNEL {         @Override         public long getResult() throws Exception {             FileInputStream fis = null;             try {                 File me = new File(FileSizeBench.class.getResource(                         'FileSizeBench.class').getFile());                 fis = new FileInputStream(me);                 return fis.getChannel().size();             } finally {                 fis.close();             }         }     },     URL {         @Override         public long getResult() throws Exception {             InputStream stream = null;             try {                 URL url = FileSizeBench.class                         .getResource('FileSizeBench.class');                 stream = url.openStream();                 return stream.available();             } finally {                 stream.close();             }         }     };      public abstract long getResult() throws Exception;      public static void main(String[] args) throws Exception {         int runs = 5;         int iterations = 50;          EnumMap<FileSizeBench, Long> durations = new EnumMap<FileSizeBench, Long>(FileSizeBench.class);          for (int i = 0; i < runs; i++) {             for (FileSizeBench test : values()) {                 if (!durations.containsKey(test)) {                     durations.put(test, 0l);                 }                 long duration = testNow(test, iterations);                 durations.put(test, durations.get(test) + duration);                 // System.out.println(test + ' took: ' + duration + ', per iteration: ' + ((double)duration / (double)iterations));             }         }          for (Map.Entry<FileSizeBench, Long> entry : durations.entrySet()) {             System.out.println();             System.out.println(entry.getKey() + ' sum: ' + entry.getValue() + ', per Iteration: ' + ((double)entry.getValue() / (double)(runs * iterations)));         }      }      private static long testNow(FileSizeBench test, int iterations)             throws Exception {         long result = -1;         long before = System.nanoTime();         for (int i = 0; i < iterations; i++) {             if (result == -1) {                 result = test.getResult();                 //System.out.println(result);             } else if ((result = test.getResult()) != result) {                  throw new Exception('variance detected!');              }         }         return (System.nanoTime() - before) / 1000;     }  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 195k
  • Answers 195k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Winforms only supports partial transparency for top-level forms. If you… May 12, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer Based off solution by Chris Lutz. Note that this doesn't… May 12, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer Question and reply is a one-to-many relationship. Reply and comment… May 12, 2026 at 6:55 pm

Related Questions

I am using bitfields to get easy access on a float library I am
I've just started using ASP, so while programming the code behind in C# I
I am currently having an issue with BackgroundWorker running on Windows Server 2003. I
Let's say I have a buggy application like this: using System; namespace ConsoleApplication1 {

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.