首页 > 编程语言 > 详细

java_24.1文件流的应用--复制文件

时间:2019-04-16 15:10:48      阅读:92      评论:0      收藏:0      [点我收藏+]

注意:先开的流要最后关

用字节流传输

public class Demo {
	public static void main(String[] args){
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("d:\\aaa.txt");
			fos = new FileOutputStream("d:\\bbb.txt");
			
			//字节输入流  读取一个字节   写一个字节
			int len =0;
			while((len=fis.read())!=-1) {
				fos.write(len);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(fos!=null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fis!=null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

 用字符数组传输

public class Demo {
	public static void main(String[] args){
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("d:\\aaa.txt");
			fos = new FileOutputStream("d:\\bbb.txt");
			
			//定义字符数组
			byte[] b = new byte[1024];
			//读取操作
			int len = 0;
			while((len = fis.read(b))!=-1) {
				fos.write(b,0,len);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(fos!=null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fis!=null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

 

java_24.1文件流的应用--复制文件

原文:https://www.cnblogs.com/smxbo/p/10698458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!