利用Java複製文件到處都可以用到,這裡總結了一個類供大家參考。裡面總共有兩個方法:
public static boolean copyFile(String srcFileName, String destFileName,boolean overlay); public static boolean copyDirectory(String srcDirName, String destDirName,boolean overlay) ;
其中:
srcFileName待複製的文件名
descFileName目標文件名
overlay如果目標文件存在,是否覆蓋如果復製成功返回true,否則返回false
代碼:
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; /** * 複製文件或文件夾* * zww */ public class CopyFileUtil { private static String MESSAGE = ""; /** * 複製單個文件* * @param srcFileName * 待複製的文件名* @param descFileName * 目標文件名* @param overlay * 如果目標文件存在,是否覆蓋* @return 如果復製成功返回true,否則返回false */ public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) { File srcFile = new File(srcFileName); // 判斷源文件是否存在if (!srcFile.exists()) { MESSAGE = "源文件:" + srcFileName + "不存在!"; JOptionPane.showMessageDialog(null, MESSAGE); return false; } else if (!srcFile.isFile()) { MESSAGE = "複製文件失敗,源文件:" + srcFileName + "不是一個文件!"; JOptionPane.showMessageDialog(null, MESSAGE); return false; } // 判斷目標文件是否存在File destFile = new File(destFileName); if (destFile.exists()) { // 如果目標文件存在並允許覆蓋if (overlay) { // 刪除已經存在的目標文件,無論目標文件是目錄還是單個文件new File(destFileName).delete(); } } else { // 如果目標文件所在目錄不存在,則創建目錄if (!destFile.getParentFile().exists()) { // 目標文件所在目錄不存在if (!destFile.getParentFile().mkdirs()) { // 複製文件失敗:創建目標文件所在目錄失敗return false; } } } // 複製文件int byteread = 0; // 讀取的字節數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(); } } } /** * 複製整個目錄的內容* * @param srcDirName * 待複製目錄的目錄名* @param destDirName * 目標目錄名* @param overlay * 如果目標目錄存在,是否覆蓋* @return 如果復製成功返回true,否則返回false */ public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay) { // 判斷源目錄是否存在File srcDir = new File(srcDirName); if (!srcDir.exists()) { MESSAGE = "複製目錄失敗:源目錄" + srcDirName + "不存在! "; JOptionPane.showMessageDialog(null, MESSAGE); return false; } else if (!srcDir.isDirectory()) { MESSAGE = "複製目錄失敗:" + srcDirName + "不是目錄! "; JOptionPane.showMessageDialog(null, MESSAGE); return false; } // 如果目標目錄名不是以文件分隔符結尾,則加上文件分隔符if (!destDirName.endsWith(File.separator)) { destDirName = destDirName + File.separator; } File destDir = new File(destDirName); // 如果目標文件夾存在if (destDir.exists()) { // 如果允許覆蓋則刪除已存在的目標目錄if (overlay) { new File(destDirName).delete(); } else { MESSAGE = "複製目錄失敗:目的目錄" + destDirName + "已存在! "; JOptionPane.showMessageDialog(null, MESSAGE); return false; } } else { // 創建目的目錄System.out.println("目的目錄不存在,準備創建。。。"); if (!destDir.mkdirs()) { System.out.println("複製目錄失敗:創建目的目錄失敗!"); return false; } } boolean flag = true; File[] files = srcDir.listFiles(); for (int i = 0; i < files.length; i++) { // 複製文件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 = "複製目錄" + srcDirName + "至" + destDirName + "失敗! "; 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.copyDirectory(srcDirName, destDirName, true); } }不考慮多線程優化,單線程文件複製最快的方法是(文件越大該方法越有優勢,一般比常用方法快30+%):
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); } }如果需要監測複製進度,可以用第二快的方法(留意buffer的大小,對速度有很大影響):
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); } }常用的方法1是:
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); } }常用的方法2是:
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); } }以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。