Java FileInputStream Copy copyFile(String sourceFile, String destDir, String newFileName)

Here you can find the source of copyFile(String sourceFile, String destDir, String newFileName)

Description

copy File

License

Apache License

Declaration

public static String copyFile(String sourceFile, String destDir, String newFileName)
        throws FileNotFoundException, IOException 

Method Source Code


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

Related

  1. copyFile(String source, String destination)
  2. copyFile(String source, String destination)
  3. copyFile(String source, String destination)
  4. copyFile(String source, String target)
  5. copyFile(String source, String targetDirectory, String filesep)
  6. copyFile(String sourceFile, String destFile)
  7. copyFile(String sourcefile, String targetfile)
  8. copyFile(String sourcefile, String targetFile)
  9. copyFile(String sourceFile, String targetFile)