Here you can find the source of copyFileToDir(File file, File destDir)
Parameter | Description |
---|---|
file | file to copy. |
destDir | dir. |
Parameter | Description |
---|
public static void copyFileToDir(File file, File destDir) throws IOException
//package com.java2s; // the terms of the GNU General Public License as published by the Free Software Foundation; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /**//from w ww . ja va 2 s . c om * Copies file from it's location to destination dir. * * @param file file to copy. * @param destDir dir. * * @throws java.io.IOException if I/O error happens. */ public static void copyFileToDir(File file, File destDir) throws IOException { String name = file.getName(); String filename = destDir.getAbsolutePath() + File.separator + name; File destFile = new File(filename); destFile.createNewFile(); copyFileToFile(file, destFile); } /** * Copies one file to another. * * @param source source file. * @param dest destination file. * * @throws java.io.IOException if I/O error happens. */ public static void copyFileToFile(File source, File dest) throws IOException { FileChannel sourceChannel = null; FileChannel targetChannel = null; try { sourceChannel = new FileInputStream(source).getChannel(); targetChannel = new FileOutputStream(dest).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), targetChannel); targetChannel.force(true); } finally { if (sourceChannel != null) sourceChannel.close(); if (targetChannel != null) targetChannel.close(); } } }