Here you can find the source of copyFile(String sourceFile, String targetFolder)
public static void copyFile(String sourceFile, String targetFolder) throws FileNotFoundException, IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2009, 2010 Progress Software Corporation. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html ******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyFile(String sourceFile, String targetFolder) throws FileNotFoundException, IOException { if (!(new File(sourceFile)).isFile()) { throw new AssertionError(); }// w w w .ja v a 2 s .co m if (!(new File(targetFolder)).isDirectory()) { throw new AssertionError(); } else { File source = new File(sourceFile); copyFile(source.getParent(), targetFolder, source.getName()); return; } } private static void copyFile(String sourceFolder, String targetFolder, String name) throws FileNotFoundException, IOException { copyFile(sourceFolder, targetFolder, name, name); } public static void copyFile(String sourceFolder, String targetFolder, String sourceName, String targetName) throws FileNotFoundException, IOException { InputStream is = new FileInputStream(getFile(sourceFolder, sourceName)); OutputStream os = new FileOutputStream(getFile(targetFolder, targetName)); byte buffer[] = new byte[102400]; do { int len = is.read(buffer); if (len >= 0) { os.write(buffer, 0, len); } else { is.close(); os.close(); return; } } while (true); } protected static String getFile(String sourceFolder, String sourceName) { return new File(sourceFolder, sourceName).getAbsolutePath(); // return (new StringBuilder(String.valueOf(sourceFolder))).append( // File.separator).append(sourceName).toString(); } }