Unknown user
Unknown user

Reputation: 45301

Copy and create files in Java

I'm trying to understand how should I use File class in order to create a new file.

Assuming that I want to copy the file D:\example\file1 and having the same file, that will be named now file2

Into this folder: D:\example.

First I created file1 By File file=new File (D:\example\file1);

Now, what should I use? a static method of File class? there are the createTempFile and the createTempFile, but I believe none of them will do I want.

Upvotes: 0

Views: 4961

Answers (1)

mKorbel
mKorbel

Reputation: 109815

f.e.

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileFolderCopy {

    public static void copy(File source, File destination) throws IOException {
        if (source == null) {
            throw new NullPointerException("Null Source");
        }
        if (destination == null) {
            throw new NullPointerException("Null Destination");
        }
        if (source.isDirectory()) {
            copyDirectory(source, destination);
        } else {
            copyFile(source, destination);
        }
    }

    public static void copyDirectory(File source, File destination) throws IOException {
        copyDirectory(source, destination, null);
    }

    public static void copyDirectory(File source, File destination, FileFilter filter) throws IOException {
        File nextDirectory = new File(destination, source.getName());
        if (!nextDirectory.exists() && !nextDirectory.mkdirs()) {// create the directory if necessary...
            Object[] filler = {nextDirectory.getAbsolutePath()};
            String message = "Dir Copy Failed";
            throw new IOException(message);
        }
        File[] files = source.listFiles();
        for (int n = 0; n < files.length; ++n) {// and then all the items below the directory...
            if (filter == null || filter.accept(files[n])) {
                if (files[n].isDirectory()) {
                    copyDirectory(files[n], nextDirectory, filter);
                } else {
                    copyFile(files[n], nextDirectory);
                }
            }
        }
    }

    public static void copyFile(File source, File destination) throws IOException {
        // what we really want to do is create a file with the same name in that dir
        if (destination.isDirectory()) {
            destination = new File(destination, source.getName());
        }
        FileInputStream input = new FileInputStream(source);
        copyFile(input, destination);
    }

    public static void copyFile(InputStream input, File destination) throws IOException {
        OutputStream output = null;
        try {
            output = new FileOutputStream(destination);
            byte[] buffer = new byte[1024];
            int bytesRead = input.read(buffer);
            while (bytesRead >= 0) {
                output.write(buffer, 0, bytesRead);
                bytesRead = input.read(buffer);
            }
        } catch (Exception e) {
            //
        } finally {
            input.close();
            output.close();
        }
        input = null;
        output = null;
    }

    private FileFolderCopy() {
    }
}

Upvotes: 1

Related Questions