public class NioTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileChannel fc = new FileInputStream("src/com/test/IOTest.java")
.getChannel();
ByteBuffer buff=ByteBuffer.allocate(1024);
try {
while(fc.read(buff)!=-1){
buff.flip();
//转换为数组
byte[] bytes=new byte[buff.remaining()];
buff.get(bytes);
//转换为String变量
String msg=new String(bytes);
System.out.print(msg);
buff.clear();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}