Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Where did my char(s) go?! FileReader read(CharBuffer target) throwing file contents away...?

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    San Francisco, California, USA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Where did my char(s) go?! FileReader read(CharBuffer target) throwing file contents away...?

    This seems to be such a straightforward procedure that I'm truly stumped as to why it's not working.

    Overall, I'm writing a program to read files from a directory and scan each file to see if a user-input text string is in the file. I'm using java.util.regex.* classes to determine whether or not the input string is present - but that's not the problem.

    The problem is reading the File (targetFile below) into a CharBuffer (which then becomes the input for the regex method).

    Here's the relevant code:
            FileReader targetFileReader;
            try {
                    targetFileReader = new FileReader(targetFile);
            }
            catch (FileNotFoundException FNFE) {
                   ....        }
     
            int fileSize =(int)targetFile.length();
            Integer charsRead = 0;
            CharBuffer charBuff = CharBuffer.allocate(fileSize);
                try {
                    charsRead = targetFileReader.read(charBuff);
                }
                catch (IOException IOE) {
                    ...            }
     
            System.out.println("Characters read into buffer: " + charsRead.toString());
            Integer bufferLength = charBuff.length();
            System.out.println("Characters now in buffer: " + bufferLength.toString());

    There are no exceptions thrown at any point, but the output is

    Characters read into buffer: 87332 (for example)
    Characters now in buffer: 0

    Where did the contents of the CharBuffer go?!?!?

    PS: Yes, I've done other tests which demonstrate that there really is nothing in the CharBuffer.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Where did my char(s) go?! FileReader read(CharBuffer target) throwing file contents away...?

    See the API for CharBuffer and its parent class Buffer. Buffer's have positions, so for instance the length method of CharBuffer states:
    When viewed as a character sequence, the length of a character buffer is simply the number of characters between the position (inclusive) and the limit (exclusive); that is, it is equivalent to remaining().
    If you've read in data, the position has changed to the number of characters read. Try resetting the position by calling rewind()

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    San Francisco, California, USA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Where did my char(s) go?! FileReader read(CharBuffer target) throwing file contents away...?

    Quote Originally Posted by copeg View Post
    See the API for CharBuffer and its parent class Buffer. Buffer's have positions, so for instance the length method of CharBuffer states:

    If you've read in data, the position has changed to the number of characters read. Try resetting the position by calling rewind()
    I knew it had to be something simple and stupid like this. It was the kind of thing I've run into more than once in Java, and always resolved in exactly this way.

    This is undoubtedly why my other tests seemed to indicate that there was nothing in the CharBuffer.

    Thanks!

Similar Threads

  1. Replies: 1
    Last Post: April 26th, 2013, 02:38 AM
  2. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  3. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  4. [SOLVED] FileReader.read() reads garbage
    By Hackworth in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 27th, 2010, 01:06 PM
  5. How to read from all files in a directory & plot the contents?
    By 123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2010, 12:43 PM

Tags for this Thread