IO字节流实现文件的拷贝基础版

  1. 没有实现程序的健壮性只是实现了一个基础功能
  2. 没有处理错误的方法

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class Test {
	public static void main(String[] args){
		copyFile("E:/1234.txt","E:/12345.txt");
	}
	public static void copyFile(String oldFile,String newFile) {
		InputStream is=null;
		OutputStream os=null;
		try {
			is=new FileInputStream(new File(oldFile));
			os=new FileOutputStream(new File(newFile));
			int len;
			byte[] by=new byte[1024];
			while ((len=is.read(by))!=0) {	//先赋值给长度在判断是否为零,如果不为0则赋值
				os.write(by, 0, len);//赋值成功后退出	
				break;
			}
		} catch (FileNotFoundException e) {	//文件未找到异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (is!=null||os!=null) {//如果输入和输出都不为空则可以关闭流
					os.flush();
					os.close();
					is.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值