Here you can find the source of copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)
public static File copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile) throws FileNotFoundException, IOException
//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 com.google.common.io.ByteStreams; public class Main { public static File copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile) throws FileNotFoundException, IOException { String relativePath = makeRelativePath(srcDir.getAbsolutePath(), originalFile.getAbsolutePath(), ""); File newFile = new File(destDir, relativePath); FileInputStream fis = new FileInputStream(originalFile); FileOutputStream fos = new FileOutputStream(newFile); ByteStreams.copy(fis, fos);// w w w. ja v a 2 s .c o m fis.close(); fos.close(); return newFile; } public static String makeRelativePath(String absoluteParentPath, String originalAbsoluteFilePath, String uniqueId) { if (originalAbsoluteFilePath != null && absoluteParentPath != null && originalAbsoluteFilePath.length() > absoluteParentPath.length()) { return originalAbsoluteFilePath.substring(absoluteParentPath.length() + 1); } else { return uniqueId; } } }