挑战100道编程题(第6道)【编写一个程序,用于实现文件的备份】

01-需求

编写一个程序,用于实现文件的备份,程序运行时的命令语法为: java MyCopy <sourcefile> <destfile>

02-思路

1)获取到两个文件的路径。

2)编写拷贝方法。

3).将a文件从a路径拷贝到b文件b路径下面。

03-代码

package com.eleven;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 实现文件的备份 思路:获取到两个文件路径。
 * 
 * @author sywangu
 *
 */
public class SevenDemo04 {
	public static void main(String[] args) throws Exception {

		File source = new File("d:\\aa\\aa.txt"); // 源目标路径
		File dest = new File("e:\\bb\\" + source.getName()); // 目标路径

		copyFile(source, dest);

	}

	/**
	 * 复制文件
	 *
	 * @param resource
	 * @param target
	 */
	public static void copyFile(File resource, File target) throws Exception {
		// 输入流 --> 从一个目标读取数据
		// 输出流 --> 向一个目标写入数据

		long start = System.currentTimeMillis();

		// 文件输入流并进行缓冲
		FileInputStream inputStream = new FileInputStream(resource);
		BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

		// 文件输出流并进行缓冲
		FileOutputStream outputStream = new FileOutputStream(target);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

		// 缓冲数组
		// 大文件 可将 1024 * 2 改大一些,但是 并不是越大就越快
		byte[] bytes = new byte[1024 * 2];
		int len = 0;
		while ((len = inputStream.read(bytes)) != -1) {
			bufferedOutputStream.write(bytes, 0, len);
		}
		// 刷新输出缓冲流
		bufferedOutputStream.flush();
		// 关闭流
		bufferedInputStream.close();
		bufferedOutputStream.close();
		inputStream.close();
		outputStream.close();

		long end = System.currentTimeMillis();

		System.out.println("耗时:" + (end - start) / 1000 + " s");

	}
}

以下是将A文件里面的内容复制到B文件中

方式一:普通方法(不推荐)
package com.eleven;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * 编写一个程序,用于实现文件的备份,程序运行时的命令语法为: java MyCopy <sourcefile> <destfile>
 * 思路:
 * 1.获取到两个文件的路径。
 * 2.将a文件从a路径拷贝到b文件b路径下面。
 * @author sywangu
 *
 */
public class SevenDemo {
	public static void main(String[] args) throws IOException {
		
		File source = new File("d:/aa/aa.txt");
		File dest = new File("e:/bb/bb.txt");
		CopyFile(source, dest);
		
	}
	// 将一个文件的内容复制到另一个文件中
	private static void CopyFile(File source, File dest) throws IOException {
		InputStream input = null;
		OutputStream output = null;
		
		try {
			input = new FileInputStream(source);	// 使用FileInputStream读取文件的字节
			output = new FileOutputStream(dest);	// 使用FileOutputStream写入到文件
			byte[] buf = new byte[1024];	// 1M
			int bytesRead;
			while( (bytesRead = input.read(buf)) > 0 ) {
				output.write(buf, 0 ,bytesRead);
			}
		}finally {
			input.close();
			output.close();
		}
		
	}
}

方式二:Java NIO中的transferFrom方法,根据文档应该比文件流复制的速度更快
package com.eleven;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class SevenDemo01 {
	public static void main(String[] args) throws IOException {
		File source = new File("d:/aa/aa.txt");
		File dest = new File("e:/bb/bb.txt");
		copyFile(source, dest);
	}

	private static void copyFile(File source, File dest) throws IOException {

		FileChannel inputChannel = null;
		FileChannel outputChannel = null;

		try {
			inputChannel = new FileInputStream(source).getChannel();
			outputChannel = new FileOutputStream(dest).getChannel();
			outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); // Java NIO中的transferFrom方法,根据文档应该比文件流复制的速度更快
		} finally {
			inputChannel.close();
			outputChannel.close();
		}

	}

}
方式三:Apache Commons IO提供拷贝文件方法在其FileUtils类
package com.eleven;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class SevenDemo02 {
	public static void main(String[] args) throws IOException {
		File source = new File("d:/aa/aa.txt");
		File dest = new File("e:/bb/bb.txt");
		copyFile(source, dest);
	}

	// Apache Commons IO提供拷贝文件方法在其FileUtils类,可用于复制一个文件到另一个地方
	private static void copyFile(File source, File dest) throws IOException {
		FileUtils.copyFile(source, dest);
	}

}
方式四:JDK7中的Files.copy方法
package com.eleven;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;

public class SevenDemo03 {
	public static void main(String[] args) throws IOException {
		File source = new File("d:/aa/aa.txt");
		File dest = new File("d:/aa/bb.txt");
		copyFile(source, dest);
	}

	// JDK7中的Files.copy方法
	private static void copyFile(File source, File dest) throws IOException {
		try {
			Files.copy(source.toPath(), dest.toPath()); // 将source里面的内容拷贝到dest中
		} catch (FileAlreadyExistsException e) {
			System.out.println(dest + "目标文件已存在");
		} catch (IOException e) {
			e.printStackTrace();	// 打印其它异常
		}
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值