Here you can find the source of copyFile(File inputFile, File outputFile)
Parameter | Description |
---|---|
inputFile | The file to be copied. |
outputFile | The directory the file should be copied to. |
public static void copyFile(File inputFile, File outputFile) throws IOException
//package com.java2s; //License from project: Open Source License 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 .j a v a 2s. c o m*/ * Copies a file to the specified location. * @param inputFile The file to be copied. * @param outputFile The directory the file should be copied to. */ public static void copyFile(File inputFile, File outputFile) throws IOException { if (inputFile.isDirectory()) { throw new IOException("Cannot copy file: " + inputFile + " as this is a directory"); } if (outputFile.isDirectory()) { outputFile = new File(outputFile, inputFile.getName()); } outputFile.getParentFile().mkdirs(); outputFile.createNewFile(); FileInputStream in = new FileInputStream(inputFile); FileOutputStream out = new FileOutputStream(outputFile); FileChannel inc = in.getChannel(); FileChannel outc = out.getChannel(); inc.transferTo(0, inc.size(), outc); inc.close(); outc.close(); in.close(); out.close(); } }