Here you can find the source of copyFile(String sourceFilename, String destFilename)
Parameter | Description |
---|---|
sourceFilename | a parameter |
destFilename | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
@SuppressWarnings("resource") public static void copyFile(String sourceFilename, String destFilename)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /*********************************************************** * @param sourceFilename/*from w w w.j av a 2 s .c o m*/ * @param destFilename * @throws IOException ***********************************************************/ @SuppressWarnings("resource") public static void copyFile(String sourceFilename, String destFilename) { File sourceFile = new File(sourceFilename); File destFile = new File(destFilename); // System.out.println("Copying file " + sourceFilename + " to " + // destFilename); if (!sourceFile.exists()) { System.out.println("ERROR: Source file " + sourceFilename + " does not exist!"); return; } try { if (!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); if (destination != null && source != null) destination.transferFrom(source, 0, source.size()); if (source != null) source.close(); if (destination == null) System.out.println("ERROR: Destination file " + destFilename + " was not created!"); else destination.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }