Copy files through JAVA NIO indirect buffer
/** * 通过JAVA NIO 非直接缓冲区拷贝文件 * * @param sourcePath 源文件路径 * @param targetPath 目标文件路径 */ public static void copyFileByChannel(String sourcePath, String targetPath) { FileChannel outChannel = null; FileChannel inChannel = null; FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(sourcePath); fos = new FileOutputStream(targetPath); //获取通道 inChannel = fis.getChannel(); outChannel = fos.getChannel(); //分配指定大小的缓冲区 ByteBuffer buf = ByteBuffer.allocate(1024); while (inChannel.read(buf) != -1) { //转换为读取数据模式 buf.flip(); //写入到磁盘 outChannel.write(buf); //清空缓冲区 buf.clear(); } } catch (Exception e) { e.printStackTrace(); } finally { //关闭流 try { if (outChannel != null) { outChannel.close(); } if (inChannel != null) { inChannel.close(); } if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } }
The above is the detailed content of File copying using Java NIO indirect buffers. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.