为什么说JAVA nio是非阻塞的
万物皆字节 2017-08-26 07:18:04
就拿复制文件来说,我看了好多资料都没有看懂到底哪里是非阻塞了。
public static void main(String[] args) throws Exception {
File src = new File("D:\\tool/iToolsSetup_4.2.0.6.exe");
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(new File("D:/iToolsSetup_4.2.0.6.exe"));
ByteBuffer buffer = ByteBuffer.allocate(1024);
FileChannel channelin = fis.getChannel();
FileChannel channelout = fos.getChannel();
while (channelin.read(buffer) != -1) {
System.out.println("--");
buffer.flip();
channelout.write(buffer);
buffer.clear();
}
fis.close();
fos.close();
System.out.println("over");
}