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

  • SEARCH
  • Home
  • 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 6965903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:07:19+00:00 2026-05-27T16:07:19+00:00

Reading file using java and jcifs on windows. I need to determine size of

  • 0

Reading file using java and jcifs on windows. I need to determine size of file, which contains multi-byte as well as ASCII characters.

how can i achieve it efficiently OR any existing API in java?

Thanks,

  • 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. Editorial Team
    Editorial Team
    2026-05-27T16:07:20+00:00Added an answer on May 27, 2026 at 4:07 pm

    To get the character count, you’ll have to read the file. By specifying the correct file encoding, you ensure that Java correctly reads each character in your file.

    BufferedReader.read() returns the Unicode character read (as an int in the range 0 to 65535). So the simple way to do it would be like this:

    int countCharsSimple(File f, String charsetName) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), charsetName));
        int charCount = 0;
        while(reader.read() > -1) {
            charCount++;
        }
        reader.close();
        return charCount;
    }
    

    You will get faster performance using Reader.read(char[]):

    int countCharsBuffer(File f, String charsetName) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), charsetName));
        int charCount = 0;
        char[] cbuf = new char[1024];
        int read = 0;
        while((read = reader.read(cbuf)) > -1) {
            charCount += read;
        }
        reader.close();
        return charCount;
    }
    

    For interest, I benchmarked these two and the nio version suggested in Andrey’s answer. I found the second example above (countCharsBuffer) to be the fastest.

    (Note that all these examples include line separator characters in their counts.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have .zip file which contain csv data. I am reading .zip file using
I am reading an xml file using javascript and then I need to submit
I've tried reading a JPG file using the StreamReader class' ReadToEnd() method which returns
I'm reading a large xml file using HttpURLConnection in java as follows. StringBuilder responseBuilder
I am reading a .wav audio file using Java AudioInputStream.The audio file is 16
I am reading a ZIP file using java as below: Enumeration<? extends ZipEntry> zes=zip.entries();
I am using Java BufferedReader and reading file line by line using bufferedReader.readline(). Is
I'm having trouble reading in supplementary unicode characters using Java. I have a file
Looking at this question: Reading Remote MP3 File's ID3 Tags using Java It looks
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();

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.