Hi,
Just playing with NIO for the first time and I am reading from text files from a collection of file objects and the reads always result in:
???
Where as if I read the same files with the old
Java IO I get the actual text.
My code:
FileInputStream inFile = null;
try {
inFile = new FileInputStream(files[i]);
}
catch(FileNotFoundException e) {
System.out.println("Eror reading file"+e);
}
FileChannel inChannel = inFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
try {
while(inChannel.read(buf) != -1) {
System.out.println("String read: "+ ((ByteBuffer)(buf.flip())).asCharBuffer().toString());
buf.clear();
}
}
catch(IOException e) {
}
Any ideas what I'm doing wrong here? I think it has to do with my byte buffer casting?:
((ByteBuffer)(buf.flip())).asCharBuffer().toString());
Thanks,
Stef