import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ExplicitChannelRead {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream fIn = null;
FileChannel fChan = null;
ByteBuffer mBuf;
int count;
try{
fIn = new FileInputStream("text.txt");
fChan = fIn.getChannel();
mBuf = ByteBuffer.allocate(128);
do{
count = fChan.read(mBuf);
if(count!=-1){
mBuf.rewind();
for(int i =0; i<count; i++)
System.out.print((char) mBuf.get());
}
}while(count!=-1);
System.out.println();
}catch(IOException e){
System.out.println("I/O Error : " + e);
}finally{
try{
if(fChan!=null)
fChan.close();
}catch(IOException e){
System.out.println("Error closing Channel.");
}
try{
if(fIn!= null)
fIn.close();
}catch(IOException e){
System.out.println("Error closing file.");
}
}
}
}
When I compile this code in Command prompt I get the error
ExplictChannelRead.java:58:error:class, interface, or enum expected }
When I compile it in my IDE I get the following error
"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at nio_path.ExplicitChannelRead.main(ExplicitChannelRead.java:12)"
I copied the entire code from a book.
Works fine for me :)
You have a hanging } after your quoted text... Make sure you have matching braces...