Here you can find the source of copyFile(File base, String relfilepath, File targetdir)
public static void copyFile(File base, String relfilepath, File targetdir) throws IOException
//package com.java2s; //License from project: LGPL import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyFile(File base, String relfilepath, File targetdir) throws IOException { File sourcefile = new File(base, relfilepath); File targetfile = new File(targetdir, relfilepath); if (!targetfile.exists()) { File parentfile = sourcefile.getParentFile(); String relpfilepath = getRelativePath(base, parentfile); File targetfiledir = new File(targetdir, relpfilepath); if (!targetfiledir.exists()) targetfiledir.mkdirs();// w ww .j a v a2s .c o m targetfile = new File(targetdir, relfilepath); } copyFile(sourcefile, targetfile); } public static void copyFile(File src, File target) throws IOException { if (!target.exists()) target.createNewFile(); if (!src.isFile() || !target.isFile()) return; FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(target); streamIO(fis, fos); fos.close(); fis.close(); } public static String getRelativePath(File base, File file) throws IOException { String basePath = base.getCanonicalPath(); String filePath = file.getCanonicalPath(); return getRelativePath(basePath, filePath); } public static String getRelativePath(String basePath, String filePath) { if (basePath.equals(filePath)) return ""; // Lets consider one type of File Separator. basePath = normalizePath(basePath); filePath = normalizePath(filePath); int beginIndex = filePath.indexOf(basePath); if (beginIndex == -1) return filePath; beginIndex += basePath.length() + 1; return filePath.substring(beginIndex); } public static void streamIO(InputStream is, OutputStream os) throws IOException { byte[] bytes = new byte[2048]; int readlen = -1; while ((readlen = is.read(bytes)) != -1) { os.write(bytes, 0, readlen); } } public static String normalizePath(String path) { String separator = "/"; return path.replaceAll("\\\\", separator); } }