Search code examples
javafile-ioioarraysfilechannel

Reading an ASCII file with FileChannel and ByteArrays


I have the following code:

        String inputFile = "somefile.txt";
        FileInputStream in = new FileInputStream(inputFile);
        FileChannel ch = in.getChannel();
        ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE);  // BUFSIZE = 256

        /* read the file into a buffer, 256 bytes at a time */
        int rd;
        while ( (rd = ch.read( buf )) != -1 ) {
            buf.rewind();
            for ( int i = 0; i < rd/2; i++ ) {
                /* print each character */
                System.out.print(buf.getChar());
            }
            buf.clear();
        }

But the characters get displayed at ?'s. Does this have something to do with Java using Unicode characters? How do I correct this?


Solution

  • You have to know what the encoding of the file is, and then decode the ByteBuffer into a CharBuffer using that encoding. Assuming the file is ASCII:

    import java.util.*;
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.charset.*;
    
    public class Buffer
    {
        public static void main(String args[]) throws Exception
        {
            String inputFile = "somefile";
            FileInputStream in = new FileInputStream(inputFile);
            FileChannel ch = in.getChannel();
            ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE);  // BUFSIZE = 256
    
            Charset cs = Charset.forName("ASCII"); // Or whatever encoding you want
    
            /* read the file into a buffer, 256 bytes at a time */
            int rd;
            while ( (rd = ch.read( buf )) != -1 ) {
                buf.rewind();
                CharBuffer chbuf = cs.decode(buf);
                for ( int i = 0; i < chbuf.length(); i++ ) {
                    /* print each character */
                    System.out.print(chbuf.get());
                }
                buf.clear();
            }
        }
    }