Here you can find the source of copyFile(File f1, File f2)
Parameter | Description |
---|---|
f1 | File |
f2 | File |
Parameter | Description |
---|---|
IOException | ex |
public static void copyFile(File f1, File f2) throws IOException
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { /**/*from ww w . ja v a2s.com*/ * Copy file * * @param f1 File * @param f2 File * @throws IOException ex */ public static void copyFile(File f1, File f2) throws IOException { if (f1 == null || f2 == null || !f1.exists()) { return; } int length = 2097152; FileInputStream in = new FileInputStream(f1); FileOutputStream out = new FileOutputStream(f2); FileChannel inC = in.getChannel(); FileChannel outC = out.getChannel(); ByteBuffer b = null; while (true) { if (inC.position() == inC.size()) { inC.close(); outC.close(); return; } if ((inC.size() - inC.position()) < length) { length = (int) (inC.size() - inC.position()); } else { length = 2097152; } b = ByteBuffer.allocateDirect(length); inC.read(b); b.flip(); outC.write(b); outC.force(false); } } }