[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

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();
    }  
}
0
peparsons
Asked:
peparsons
  • 5
  • 4
1 Solution
 
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
 
aozarovCommented:
Or probably you want to call
CharBuffer buf = CharBuffer.allocate(INPUT_BUFFER_SIZE );
insted of having it as ByteBuffer.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
aozarovCommented:
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now