Results 1 to 3 of 3
  1. #1
    testarosa is offline Member
    Join Date
    Feb 2009
    Location
    São Paulo, Brazil
    Posts
    4
    Rep Power
    0

    Default Problems with BufferedReader

    I dont know what is going on with my program. I read from a file like this:

    File foo = new File(args[0]);
    BufferedReader in = new BufferedReader(new FileReader(foo));

    i=0;
    while(i<6){
    char[] buffer= new char[n];
    qt= in.read(buffer, offset, n);
    System.out.println("***How many characters should be read into buffer: " + n);
    System.out.println("***How many were actually read: " + qt);
    i++;
    }

    And here is my output:

    ***How many characters should be read into buffer: 7678
    ***How many were actually read: 7678

    ***How many characters should be read into buffer: 7678
    ***How many were actually read: 514

    ***How many characters should be read into buffer: 7677
    ***How many were actually read: 7677

    ***How many characters should be read into buffer: 7677
    ***How many were actually read: 515

    ***How many characters should be read into buffer: 7677
    ***How many were actually read: 7677

    ***How many characters should be read into buffer: 7677
    ***How many were actually read: 515

    The size of my input file is (7678+7678+7677+7677+7677+7677), as you can see above.
    Has anyone already had this problem?

    Thanks in advance.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    And? If you read the API docs for read, carefully, you will see that it does not guaranteed to fill that passed in buffer.

    do
    Java Code:
    int totalRead = 0;
    int byteRead = 0;
    while ((totalRead < buffer.length) && ((byteRead = in.read(buffer, totalRead, buffer.length - totalRead)) >= 0)) {
        totalRead += byteRead;
    }

  3. #3
    testarosa is offline Member
    Join Date
    Feb 2009
    Location
    São Paulo, Brazil
    Posts
    4
    Rep Power
    0

    Default thanx!

    I do appreciate your help. It introduced a new loop in my program (increasing overhead) but it solved my problem.
    Thank You.

Similar Threads

  1. How to use BufferedReader?
    By ProgramBeginner in forum New To Java
    Replies: 4
    Last Post: 10-11-2011, 02:56 PM
  2. BufferedReader
    By vidhya.sk in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 01:57 PM
  3. Help with BufferedReader in java
    By carl in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 07:20 AM
  4. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:52 PM
  5. BufferedReader: readLine method problems
    By bbq in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2007, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •