Java File Copy copy(File from, File to)

Here you can find the source of copy(File from, File to)

Description

copy

License

Open Source License

Declaration

public static void copy(File from, File to) 

Method Source Code

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

import java.io.*;

import javax.swing.JOptionPane;

public class Main {
    public static void copy(File from, File to) {
        try {//from w  w  w .  j  ava2 s . co  m
            File w = new File(to, from.getName());
            if (!w.exists())
                w.createNewFile();
            BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(w));
            BufferedInputStream read = new BufferedInputStream(new FileInputStream(from));
            byte[] buffer = new byte[1024];
            int l = 0;
            while ((l = read.read(buffer)) > -1) {
                write.write(buffer, 0, l);
            }
            write.flush();
            write.close();
            read.close();
            System.out.println("Copied " + from.getName() + " to " + to.getCanonicalPath());
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Could not copy file " + from.getName() + ": " + e.getMessage(),
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File src, File dest, Component parentComponent, String message)
  3. copyFileFromFileSystem(String sourceFileName, File target)
  4. copyFiles(File file, File destPath)