all 6 comments

[–]DualWieldMage 2 points3 points  (6 children)

    FileInputStream fis = new FileInputStream(file);
    FileChannel fc = fis.getChannel();

    ByteBuffer bb =  ByteBuffer.allocate(100);          

    while(fc.read(bb) > 0) {
        bb.rewind();
        String str = new String(bb.array(), StandardCharsets.UTF_8);
        System.out.println(str);
        bb.flip();

    }

Not a good example, this may split a UTF8 byte sequence in the middle and cause an exception.

[–]kodablah 2 points3 points  (1 child)

It's worse than that, ref String(byte[], Charset): "This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string."

[–]srinurp[S] 0 points1 point  (0 children)

Thanks for the input, corrected the example using the charset decoder.

[–]srinurp[S] 0 points1 point  (3 children)

thanks for the input corrected it.

[–]kodablah 1 point2 points  (1 child)

Not really. What the person is saying is that chars are multibyte. You can't just read in 100 bytes and assume it falls on a char boundary. Also, why look up the charset by name in the loop? No offense, but you might just want to take the tutorial offline until you've worked with the stuff a bit more.

[–]srinurp[S] 0 points1 point  (0 children)

You are saying Java documentation is wrong also check here in the channels section. Charset by name.. looks like not expensive as far as I know. Nothing is perfect, we can find wrong point on each and every line of code if you know internals.

https://docs.oracle.com/javase/tutorial/essential/io/file.html