Java Utililty Methods FileInputStream Copy

List of utility methods to do FileInputStream Copy

Description

The list of methods to do FileInputStream Copy are organized into topic(s).

Method

voidcopyFile(File sourceFile, File destFile)
copy File
FileInputStream inStream = new FileInputStream(sourceFile);
try {
    FileOutputStream outStream = new FileOutputStream(destFile);
    try {
        inStream.getChannel().transferTo(0, sourceFile.length(), outStream.getChannel());
    } finally {
        outStream.close();
} finally {
    inStream.close();
voidcopyFile(File sourceFile, File destFile)
copy File
if (!destFile.exists()) {
    destFile.createNewFile();
FileInputStream from = null;
FileOutputStream to = null;
try {
    from = new FileInputStream(sourceFile);
    to = new FileOutputStream(destFile);
...
voidcopyFile(File sourceFile, File destFile)
Copies the source file to the destination file Also creates any interim directories needed
if (!sourceFile.isFile())
    throw new Exception("ERROR NOT A FILE: " + sourceFile.getPath());
File destParent = new File(destFile.getParent());
if (!destParent.exists()) {
    destParent.mkdirs();
FileInputStream from = null;
FileOutputStream to = null;
...
voidcopyFile(File sourceFile, File destFile, boolean overwrite, boolean preserveLastModified)
Method to copy a file from a source to a destination specifying if source files may overwrite newer destination files and the last modified time of destFile file should be made equal to the last modified time of sourceFile.
if (overwrite || !destFile.exists() || destFile.lastModified() < sourceFile.lastModified()) {
    if (destFile.exists() && destFile.isFile())
        destFile.delete();
    File parent = new File(destFile.getParent());
    if (!parent.exists())
        parent.mkdirs();
    FileInputStream in = new FileInputStream(sourceFile);
    FileOutputStream out = new FileOutputStream(destFile);
...
voidcopyFile(File sourceFile, File destinationFile)
Copies the specified source file's content to the destination file's location.
InputStream in = null;
OutputStream out = null;
try {
    if (!destinationFile.getParentFile().exists()) {
        destinationFile.getParentFile().mkdirs();
    in = new FileInputStream(sourceFile);
    out = new FileOutputStream(destinationFile);
...
voidcopyFile(File sourceFile, File destinationFile)
A method that creates a copy of file at new file path
destinationFile.getParentFile().mkdirs();
InputStream is = new FileInputStream(sourceFile);
try {
    redirectInputStream2File(is, destinationFile);
} catch (IOException e) {
    if (destinationFile.exists()) {
        destinationFile.delete();
    is.reset();
    redirectInputStream2File(is, destinationFile);
booleancopyFile(File sourceFile, File targetDir)
copy File
File targetFile = new File(targetDir, sourceFile.getName()); 
if (sourceFile.isDirectory()) { 
    targetFile.mkdir();
    File[] dir = sourceFile.listFiles();
    for (int i = 0; i < dir.length; i++) {
        if (!copyFile(dir[i], targetFile)) {
            return false;
    return true;
} else { 
    FileInputStream fis = new FileInputStream(sourceFile);
    FileOutputStream fos = new FileOutputStream(targetFile);
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int c = 0;
    while ((c = fis.read(buffer)) != -1) {
        fos.write(buffer, 0, c);
    fis.close();
    fos.close();
    return true;
voidcopyFile(File sourceFile, File targetDir)
Copy a file on the file system from a source to a destination
if (!targetDir.exists()) {
    targetDir.mkdirs();
File copy = new File(targetDir.getAbsolutePath() + File.separatorChar + sourceFile.getName());
InputStream is = null;
if (!copy.exists()) {
    try {
        is = new FileInputStream(sourceFile);
...
booleancopyFile(File sourceFile, File targetFile)
copy File
if (sourceFile == null || !sourceFile.isFile() || targetFile == null) {
    return false;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
    inputStream = new FileInputStream(sourceFile);
    outputStream = new FileOutputStream(targetFile);
...
voidcopyFile(File sourceFile, File targetFile)
copy File
if (sourceFile.isDirectory()) {
    copyDir(sourceFile, targetFile);
} else {
    File parent = targetFile.getParentFile();
    if (!parent.exists()) {
        if (!parent.mkdirs()) {
            throw new IOException("Failed to create directory " + parent.getAbsolutePath());
    final InputStream is = new FileInputStream(sourceFile);
    final OutputStream os = new FileOutputStream(targetFile);
    copyStreamAndClose(is, os);