Here you can find the source of copyFile(String input, String output)
Parameter | Description |
---|---|
input | - the file to be copied |
output | - the destination file |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFile(String input, String output) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.channels.FileChannel; public class Main { /**/*from ww w .ja v a 2 s . co m*/ * copies a file from input to output * * @param input * - the file to be copied * @param output * - the destination file * @throws IOException */ public static void copyFile(String input, String output) throws IOException { // Open the file to be copied BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(input))); // And the one where to copy it to BufferedWriter out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(output))); String line = null; while ((line = in.readLine()) != null) { out.write(line); out.newLine(); } // close the streams in.close(); out.close(); } public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); // previous code: destination.transferFrom(source, 0, // source.size()); // to avoid infinite loops, should be: long count = 0; long size = source.size(); while ((count += destination.transferFrom(source, count, size - count)) < size) ; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } } }