Reading a text file

I'm using some code based on some from a book to try to read a text file and print it out, but it doesn't seem to work.
I've simplified it as much as I can, including removing the exception handling etc.

When I step through it in the debugger, it seems to go wrong when it converts the Charbuffer to a string. The buffer in ByteBuffer looks OK to me. But when it converts it to a String, instead of there being 10 ascii characters, there are just 5 characters that look to me like Chinese.

My question is this... what's wrong with the code, please?

Thanks.

---------------------------------------------

package MyTest;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class TestFileIO
{
    private static final String TEST_FILENAME = "C:/tmp/test.txt";
    private static final int INPUT_BUFFER_SIZE = 10;
   
    /** Creates a new instance of TestFileIO */
    public TestFileIO ()
    {
    }
   
    public static void main (String[] args) throws Exception
    {
        File file = new File ( TEST_FILENAME );
        FileInputStream inputFile = null;
        inputFile = new FileInputStream ( file );
        FileChannel inChannel = inputFile.getChannel();
        ByteBuffer buff = ByteBuffer.allocate ( INPUT_BUFFER_SIZE );
        while ( inChannel.read ( buff ) != -1 ) // read next chunk from file
        {
            buff.flip ();
            CharBuffer charBuffer = buff.asCharBuffer ();
            String str = charBuffer.toString ();
            System.out.println ( str );
            buff.clear();
        }
        System.out.println ( "EOF reached" );
        inputFile.close();
    }  
}
peparsonsAsked:
Who is Participating?
 
aozarovConnect With a Mentor Commented:
Right it is not that simple (becuae FileChannel accepts only ByteBuffer).
That is still doable but try  this before:

Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buff);

instead of
CharBuffer charBuffer = buff.asCharBuffer ();
0
 
aozarovCommented:
ByteBuffer buff = ByteBuffer.allocate ( INPUT_BUFFER_SIZE );
10 bytes equals to 5 chars.
0
 
aozarovCommented:
if you want to read 10 characters at a time you need to double your INPUT_BUFFER_SIZE.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
aozarovCommented:
Or probably you want to call
CharBuffer buf = CharBuffer.allocate(INPUT_BUFFER_SIZE );
insted of having it as ByteBuffer.
0
 
peparsonsAuthor Commented:
Thanks for responding, aozarov, but the number of characters isn't the issue.
The problem is that the characters are wrong.
(They get printed out as little empty squares, but when I look in the debugger in NetBEans, they look Chinese.)

Paul
0
 
peparsonsAuthor Commented:
OK - are you saying that I should make it read into a CharBuffer instead of a ByteBuffer?
0
 
peparsonsAuthor Commented:
I tried changing the ByteBuffer into a CharBuffer, and it won't compile.
It's complained that there's no read method which takes a CharBuffer as an argument.
0
 
peparsonsAuthor Commented:
I wasn't hopefuly, but I've tried it and...    it works!

Thanks - I was getting really desparate.

You got the points :-)
0
 
aozarovCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.