IO流之copy任意文件

编写一个方法实现拷贝任意文件的功能(可拷贝任意类型的文件),要求效率最高
提示:
1.使用缓冲流
2.通过数组的方式读写

分析 : 因为要复制的是任意文件,使用字节流进行文件复制
直接输入路径和文件名,实现任意文件的复制

代码:

public class CopyFile {

	public static void main(String[] args) throws IOException {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入文件路径:");
		String path1 = sc.next();
		
		File f = new File(path1);
		String path = f.toString();
		
//		System.out.println(path);
		
		String[] ss = path.split("\\.");
		String hz = ss[ss.length-1];
		
//		System.out.println(hz);
		
		File f2 = new File("E:\\aa\\456."+hz);
		
//		String path1 = f2.toString();
//		System.out.println(path1);
		
		// 1. 先确定从哪个文件中复制内容,绑定一个数据源
		FileInputStream fis = new FileInputStream(f);
		BufferedInputStream bis = new BufferedInputStream(fis);
		// 2.确定将图片内容复制到哪里去,绑定一个数据目的
		FileOutputStream fos = new FileOutputStream(f2);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		// 3.进行文件的复制
		byte[] b = new byte[1024];
		int len;
		// 5. 边读边写
		while ((len=bis.read(b))!=-1) {
				bos.write(b);
		}
		// 6. 关闭资源
		bis.close();
		bos.close();
		
		
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值