如何运用NIO文件流技术实现长尾词的文件高效复制操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计106个文字,预计阅读时间需要1分钟。
javapublic static void nioCopyFile(String resource, String destination) throws IOException { Files.copy(Paths.get(resource), Paths.get(destination));}
NIO与文件流合作复制文件public static void nioCopyFile(String resource, String destination) throws IOException { FileInputStream fis = new FileInputStream(resource); FileOutputStream fos = new FileOutputStream(destination); FileChannel readChannel = fis.getChannel(); //读文件通道 FileChannel writeChannel = fos.getChannel(); //写文件通道 ByteBuffer buffer = ByteBuffer.allocate(1024); //读入数据缓存 while (true) { buffer.clear(); int len = readChannel.read(buffer); //读入数据 if (len == -1) { break; //读取完毕 } buffer.flip(); writeChannel.write(buffer); //写入文件 } readChannel.close(); writeChannel.close(); }
本文共计106个文字,预计阅读时间需要1分钟。
javapublic static void nioCopyFile(String resource, String destination) throws IOException { Files.copy(Paths.get(resource), Paths.get(destination));}
NIO与文件流合作复制文件public static void nioCopyFile(String resource, String destination) throws IOException { FileInputStream fis = new FileInputStream(resource); FileOutputStream fos = new FileOutputStream(destination); FileChannel readChannel = fis.getChannel(); //读文件通道 FileChannel writeChannel = fos.getChannel(); //写文件通道 ByteBuffer buffer = ByteBuffer.allocate(1024); //读入数据缓存 while (true) { buffer.clear(); int len = readChannel.read(buffer); //读入数据 if (len == -1) { break; //读取完毕 } buffer.flip(); writeChannel.write(buffer); //写入文件 } readChannel.close(); writeChannel.close(); }

