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();
}
}
}