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,
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.
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:
You will get faster performance using Reader.read(char[]):
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.)