Use Java NIO to copy file
private void copyFile(String source, String destination) throws IOException { FileInputStream fis = new FileInputStream(source); FileChannel fci = fis.getChannel(); FileOutputStream fos = new FileOutputStream(destination); FileChannel fco = fos.getChannel(); // create a buffer with 2048 size for buffering data ByteBuffer buffer = ByteBuffer.allocate(2048); int b = fci.read(buffer); while(b != -1) { // reach the end of the channel? // flip the buffer buffer.flip(); // write to the destination channel fco.write(buffer); // clear the buffer and use it for the next read process buffer.clear(); // read a block of data and put it in the buffer b = fci.read(buffer); } fis.close(); fos.close(); }
posted on 2012-09-05 21:57 Xingning Ou 阅读(205) 评论(0) 编辑 收藏 举报