码迷,mamicode.com
首页 > 编程语言 > 详细

Java 杂记

时间:2019-01-16 16:39:20      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:span   两种   long   public   name   ati   static   pos   rto   

 

文件拷贝两种方式

//不断内存拷贝,大文件增加cpu负担
public static void copyFile(String srcFile, String destFile) throws Exception {
    byte[] temp = new byte[1024];
    FileInputStream in = new FileInputStream(srcFile);
    FileOutputStream out = new FileOutputStream(destFile);
    int length;
    while ((length = in.read(temp)) != -1) {
        out.write(temp, 0, length);
    }
    in.close();
    out.close();
}
//减少内存拷贝
public static void copyFileWithFileChannel(String srcFileName, String destFileName) throws Exception {
    RandomAccessFile srcFile = new RandomAccessFile(srcFileName, "r");
    FileChannel srcFileChannel = srcFile.getChannel();
    RandomAccessFile destFile = new RandomAccessFile(destFileName, "rw");
    FileChannel destFileChannel = destFile.getChannel();
    long position = 0;
    long count = srcFileChannel.size();
    srcFileChannel.transferTo(position, count, destFileChannel);
}

 

Java 杂记

标签:span   两种   long   public   name   ati   static   pos   rto   

原文地址:https://www.cnblogs.com/sunkaikees/p/10277232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!