Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how determining file size in term of number of characters?

Tags:

java

jcifs

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,

like image 376
Sach Avatar asked Oct 14 '25 16:10

Sach


1 Answers

No doubts, to get exact number of characters you have to read it with proper encoding. The question is how to read files efficiently. Java NIO is fastest known way to do that.

FileChannel fChannel = new FileInputStream(f).getChannel();
    byte[] barray = new byte[(int) f.length()];
    ByteBuffer bb = ByteBuffer.wrap(barray);
    fChannel.read(bb);

then

String str = new String(barray, charsetName);
str.length();

Reading into byte buffer is done with a speed near to maximum available ( for me it was like 60 Mb/sec while disk speed test gives about 70-75 Mb/sec)

like image 114
andrey Avatar answered Oct 17 '25 05:10

andrey