Here you can find the source of copyFile(String sourceFile, String destDir, String newFileName)
public static String copyFile(String sourceFile, String destDir, String newFileName) throws FileNotFoundException, IOException
//package com.java2s; //License from project: Apache License import java.io.*; public class Main { public static String copyFile(String sourceFile, String destDir, String newFileName) throws FileNotFoundException, IOException { return copyFile(new FileInputStream(sourceFile), destDir, newFileName); }/*from w w w . jav a 2 s . co m*/ public static String copyFile(InputStream source, String destDir, String newFileName) throws IOException { File dir = new File(destDir); if (!dir.exists()) { dir.mkdirs(); } if (!dir.isDirectory()) { throw new IOException("dest dir (" + destDir + ") is not a folder"); } String destFileFullName = null; BufferedOutputStream out = null; try { destFileFullName = destDir + File.separator + newFileName; out = new BufferedOutputStream(new FileOutputStream(destFileFullName)); byte[] buffer = new byte[8192]; int bytesRead = 0; while ((bytesRead = source.read(buffer, 0, 8192)) != -1) { out.write(buffer, 0, bytesRead); } } finally { if (out != null) { out.close(); } } return destFileFullName; } public static void mkdirs(String destDir) { File dir = new File(destDir); if (!dir.exists()) { dir.mkdirs(); } } }