java 实现文件复制,二种方法.
来源:互联网 发布:浪潮java 编辑:程序博客网 时间:2024/06/02 05:13
Java复制文件 下面介绍两种方法
一种是使用传统的缓冲输入输出流(InputStream、OutputStream)来实现
另外一种使用文件通道(FileChannel)来实现,效率上FileChannel会比InputStream快
而且文件越大对比越明显
/** * 缓冲输入输出流方式复制文件 * @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()) { try { throw new Exception("源文件:" + srcFileName + "不存在!"); } catch (Exception e) { e.printStackTrace(); } return false; } else if (!srcFile.isFile()) { try { throw new Exception("复制文件失败,源文件:" + srcFileName + "不是一个文件!"); } catch (Exception e) { e.printStackTrace(); } 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(); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
二、文件通道(FileChannel)
/** * 使用文件通道的方式复制文件 * @param srcDirName 待复制的文件名 * @param destDirName 目标文件名 */public static void fileChannelCopy(String srcDirName, String destDirName) { FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(new File(srcDirName)); fo = new FileOutputStream(new File(destDirName)); in = fi.getChannel(); out = fo.getChannel(); in.transferTo(0, in.size(), out); } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
测试代码
public static void main(String[] args) { String srcDirName = "D:/jdk-6u2-windows-i586-p.exe"; String destDirName = "D:/jdk-6u2-windows-i586-p-bak.exe"; long start; long end; start = System.currentTimeMillis(); CopyFile.copyFile(srcDirName, destDirName, true); end = System.currentTimeMillis(); System.out.println("缓冲输入输出流方式复制文件 用时:" + (end - start) + " ms"); start = System.currentTimeMillis(); CopyFile.fileChannelCopy(srcDirName, destDirName); end = System.currentTimeMillis(); System.out.println("使用文件通道的方式复制文件 用时:" + (end - start) + " ms");}
输出结果