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.