java实现文件拷贝的三种方式

 

package com.zmm.copy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


public class IOcopyTest {

	public static void main(String[] args) throws IOException {

		File sourceFile = new File("D://poju.mp4");

		File targetFile = new File("D://666.mp4");
		copyFileByStream(sourceFile, targetFile);

		// copyFileByChannel(sourceFile, targetFile);

		// copyFileByFilesClass("D://poju.mp4", "D://896.mp4");
	}

	/**
	 * 方式1  java.io类库中的传统输入输出流
	 * 
	 * @param source
	 * @param target
	 * @throws IOException
	 */
	private static void copyFileByStream(File source, File target) throws IOException {

		long start = System.currentTimeMillis();

		try (InputStream fileInputStream = new FileInputStream(source);
				OutputStream fileOutputStream = new FileOutputStream(target);) {

			byte[] buffer = new byte[1024];

			int length;
			int i = 0;
			while ((length = fileInputStream.read(buffer)) > 0) {
				fileOutputStream.write(buffer, 0, length);
				i++;
			}
			System.out.println("i+++=" + i);
			System.out.println("cost time:" + (System.currentTimeMillis() - start));
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	/**
	 * 方式2   java.nio类库中的文件通道方式 transferTo/transferFROM
	 * 
	 * @param source
	 * @param target
	 * @throws IOException
	 */
	private static void copyFileByChannel(File source, File target) throws IOException {

		long start = System.currentTimeMillis();

		try (FileChannel inputChannel = new FileInputStream(source).getChannel();
				FileChannel outputChannel = new FileOutputStream(target).getChannel();) {

			int i = 0;

			for (long count = inputChannel.size(); count > 0;) {
				// long transferTo = outputChannel.transferFrom(inputChannel,
				// inputChannel.position(), count);
				long transferTo = inputChannel.transferTo(inputChannel.position(), count, outputChannel);

				count -= transferTo;
				i++;
			}

			System.out.println("i=" + i);
			System.out.println("cost time:" + (System.currentTimeMillis() - start));
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	/**
	 * 方式3  java 标准类库中的Files.copy方法
	 * 
	 * @param sourcePath
	 * @param targetPath
	 */

	private static void copyFileByFilesClass(String sourcePath, String targetPath) {
		long start = System.currentTimeMillis();
		Path path = Paths.get(sourcePath);
		try {
			Files.copy(path, new FileOutputStream(new File(targetPath)));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("cost time:" + (System.currentTimeMillis() - start));
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值