public static void fileCopyNIO(String source, String target) throws IOException {try (FileInputStream in = new FileInputStream(source)) {try (FileOutputStream out = new FileOutputStream(target)) {FileChannel inChannel = in.getChannel();FileChannel outChannel = out.getChannel();//创建字节缓存对象ByteBuffer buffer = ByteBuffer.allocate(4096);while(inChannel.read(buffer) != -1) {//切换到读的模式buffer.flip();outChannel.write(buffer);//清除数据buffer.clear();}}}}
【java】NIO实现文件的拷贝
于 2024-01-13 22:01:36 首次发布
本文介绍了如何使用JavaNIO(NewInput/Output)API中的FileCopyNIO方法进行文件的高效复制,包括FileInputStream,FileOutputStream和FileChannel的配合操作。
摘要由CSDN通过智能技术生成