Java FileInputStream Copy copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)

Here you can find the source of copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)

Description

copy File To Another Dir With Relative Paths

License

Open Source License

Declaration

public static File copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)
            throws FileNotFoundException, IOException 

Method Source Code


//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;
        }
    }
}

Related

  1. copyFileFromFileToFile(File fromFile, File toFile)
  2. copyFileFromTo(String filename, File fromDir, File toDir)
  3. copyFileLowLevel(File source, File target)
  4. copyFileR(File srcFile, File destDirectory)
  5. copyFileRecursive(File src, File dest)
  6. copyFileToContainer(File pChild, IContainer pContainer)
  7. copyFileToDestDir(String srcFilePath, String destFileDir)
  8. copyFileToDir(File oldFile, File outputDir)
  9. copyFileToDir(File sourceFile, File destDir)