Java FileInputStream Copy copyFile(File sourceFile, String newFileName)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File sourceFile, String newFileName)
            throws java.io.FileNotFoundException, java.io.IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileOutputStream;

public class Main {
    public static void copyFile(File sourceFile, String newFileName)
            throws java.io.FileNotFoundException, java.io.IOException {
        File newFile = new File(newFileName);
        copyFile(sourceFile, newFile);/*from ww  w.  ja v  a2 s. c  o  m*/
    }

    public static void copyFile(File sourceFile, File newFile)
            throws java.io.FileNotFoundException, java.io.IOException {
        java.io.FileInputStream input = new java.io.FileInputStream(sourceFile);
        if (!newFile.exists()) {
            newFile.createNewFile();
        }
        java.io.FileOutputStream output = new FileOutputStream(newFile);

        byte buffer[] = new byte[1024];
        int noRead = 0;

        noRead = input.read(buffer, 0, 1024);

        //Write out the stream to the file
        while (noRead != -1) {
            output.write(buffer, 0, noRead);
            noRead = input.read(buffer, 0, 1024);
        }
        output.flush();
        output.close();
        input.close();

    }

    public static boolean exists(String path, String fileNameWithoutFullPath) {
        String filePath = getFileNameWithPath(path, fileNameWithoutFullPath);
        return exists(filePath);
    }

    public static boolean exists(String fileNameWithFullPath) {
        File file = new File(fileNameWithFullPath);
        return file.exists();
    }

    public static String getFileNameWithPath(String path, String fileNameWithoutFullPath) {
        return path + File.separator + fileNameWithoutFullPath;
    }
}

Related

  1. copyFile(File sourceFile, File destinationFile)
  2. copyFile(File sourceFile, File targetDir)
  3. copyFile(File sourceFile, File targetDir)
  4. copyFile(File sourceFile, File targetFile)
  5. copyFile(File sourceFile, File targetFile)
  6. copyFile(File sourceLocation, File targetLocation)
  7. copyFile(File src, File dest)
  8. copyFile(File src, File dest)
  9. copyFile(File src, File dest)