Copying files using Java can be used everywhere. Here is a class for your reference. There are two methods in total:
public static boolean copyFile(String srcFileName, String destFileName,boolean overlay); public static boolean copyDirctory(String srcDirName, String destDirName,boolean overlay);
in:
srcFileName file name to be copied
descFileName target file name
overlay If the target file exists, whether to overwrite it. If the copy is successful, return true, otherwise return false
Code:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.swing.JOptionPane; /** * Copy file or folder* * zww */ public class CopyFileUtil { private static String MESSAGE = ""; /** * Copy a single file* * @param srcFileName * File name to be copied* @param descFileName * Target file name* @param overlay * If the target file exists, whether to overwrite* @return If the copy is successful, return true, otherwise return false */ public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) { File srcFile = new File(srcFileName); // Determine whether the source file exists if (!srcFile.exists()) { MESSAGE = "Source file:" + srcFileName + "Not exist!"; JOptionPane.showMessageDialog(null, MESSAGE); return false; } else if (!srcFile.isFile()) { MESSAGE = "Copy file failed, source file: " + srcFileName + "Not a file!"; JOptionPane.showMessageDialog(null, MESSAGE); return false; } // Determine whether the target file exists File destFile = new File(destFileName); if (destFile.exists()) { // If the target file exists and allows overwriting if (overlay) { // Delete the existing target file, regardless of whether the target file is a directory or a single file new File(destFileName).delete(); } } else { // If the directory where the target file does not exist, create the directory if (!destFile.getParentFile().exists()) { // The directory where the target file does not exist if (!destFile.getParentFile().mkdirs()) { // Failed to copy the file: failed to create the directory where the target file is returned false; } } } // Copy the file int byteread = 0; // Number of bytes read InputStream in = null; OutputStream out = null; try { in = new FileInputStream(srcFile); out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; while ((byteread = in.read(buffer)) != -1) { out.write(buffer, 0, byteread); } return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * Copy the content of the entire directory* * @param srcDirName * Directory name of the directory to be copied* @param destDirName * Destination directory name* @param overlay * If the target directory exists, whether to overwrite* @return If the copy is successful, return true, otherwise false */ public static boolean copyDirctory(String srcDirName, String destDirName, boolean overlay) { // Determine whether the source directory exists File srcDir = new File(srcDirName); if (!srcDir.exists()) { MESSAGE = "Copy directory failed: source directory" + srcDirName + "Don't exist! "; JOptionPane.showMessageDialog(null, MESSAGE); return false; } else if (!srcDir.isDirectory()) { MESSAGE = "Copy directory failed: " + srcDirName + "Not a directory! "; JOptionPane.showMessageDialog(null, MESSAGE); return false; } // If the target directory name does not end with a file delimiter, add the file delimiter if (!destDirName.endsWith(File.separator)) { destDirName = destDirName + File.separator; } File destDir = new File(destDirName); // If the target folder exists if (destDir.exists()) { // If overlay is allowed, delete the existing target directory if (overlay) { new File(destDirName).delete(); } else { MESSAGE = "Copy directory failed: Destination directory" + destDirName + "Already existed! "; JOptionPane.showMessageDialog(null, MESSAGE); return false; } } else { // Create the destination directory System.out.println("The destination directory does not exist, ready to create... "); if (!destDir.mkdir()) { System.out.println("Copy directory failed: Creating the destination directory failed!"); return false; } } boolean flag = true; File[] files = srcDir.listFiles(); for (int i = 0; i < files.length; i++) { // Copy file if (files[i].isFile()) { flag = CopyFileUtil.copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), overlay); if (!flag) break; } else if (files[i].isDirectory()) { flag = CopyFileUtil.copyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName(), overlay); if (!flag) break; } } if (!flag) { MESSAGE = "Copy Directory" + srcDirName + "To" + destDirName + "Failed! "; JOptionPane.showMessageDialog(null, MESSAGE); return false; } else { return true; } } public static void main(String[] args) { String srcDirName = "C:/test/test0/test1"; String destDirName = "c:/ttt"; CopyFileUtil.copyDirctory(srcDirName, destDirName, true); } }Without considering multi-thread optimization, the fastest way to copy single-threaded files is (the larger the file, the more advantage it is, it is generally 30+% faster than the commonly used methods):
private static void nioTransferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(target); in = inStream.getChannel(); out = outStream.getChannel(); in.transferTo(0, in.size(), out); } catch (IOException e) { e.printStackTrace(); } finally { close(inStream); close(in); close(outStream); close(out); } }If you need to monitor the replication progress, you can use the second fastest method (pay attention to the size of the buffer, which has a great impact on speed):
private static void nioBufferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(target); in = inStream.getChannel(); out = outStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(4096); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } finally { close(inStream); close(in); close(outStream); close(out); } }Commonly used method 1 is:
private static void customBufferBufferedStreamCopy(File source, File target) { InputStream fis = null; OutputStream fos = null; try { fis = new BufferedInputStream(new FileInputStream(source)); fos = new BufferedOutputStream(new FileOutputStream(target)); byte[] buf = new byte[4096]; int i; while ((i = fis.read(buf)) != -1) { fos.write(buf, 0, i); } } catch (Exception e) { e.printStackTrace(); } finally { close(fis); close(fos); } }Commonly used method 2 is:
private static void customBufferStreamCopy(File source, File target) { InputStream fis = null; OutputStream fos = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(target); byte[] buf = new byte[4096]; int i; while ((i = fis.read(buf)) != -1) { fos.write(buf, 0, i); } } catch (Exception e) { e.printStackTrace(); } finally { close(fis); close(fos); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.