Java_141_IO_封装拷贝(文件到文件,文件到字节数组,字节数组到文件)_封装释放_可变参数_try(自动释放)_Closeable接口_Flushable接口

封装拷贝

封装释放

可变参数(typeName... name)

自动释放 try...with...resource 

例:try(InputStream in=new FileInputStream(path);OutputStream out=new FileOutputStream(path)){

code...

}catch{}

不要写finally

public abstract class InputStream implements Closeable //Closeable 接口

public abstract class OutputStream implements Closeable, Flushable //Flushable 接口

package IOStudy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 1.封装拷贝
 * 2.封装释放
 * 
 * 可变参数
 * name(name... x)
 * 
 * try...with...resource 自动释放
 * @author pmc
 *
 */
public class FileUtilsIO {
	public static void main(String[] args) {
		//文件到文件
		try {
			InputStream in=new FileInputStream("txt.txt");
			OutputStream out = new FileOutputStream("txts.txt");
			copy(in,out);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		//文件到字节数组
		byte[] datas=null;
		try {
			InputStream in=new FileInputStream("abc.png");
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			copy(in,out);
			datas=out.toByteArray();
			System.out.println(datas.length);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		//字节数组到文件
		try {
				InputStream in=new ByteArrayInputStream(datas);
				OutputStream out = new FileOutputStream("abcd.png");
				copy(in,out);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
	}
	
	
/**
 * 对接输入输出流
 * @param in
 * @param out
 */
 private static void copy(InputStream in,OutputStream out){
	 try{
		 byte[] temp=new byte[1024];
		 int len = 0;
		 while((len=in.read(temp))!=-1){
			 out.write(temp,0,len);
		 }
		 out.flush();
	 }catch(IOException e){
		 e.printStackTrace();
	 }finally{
		 //先关闭写入,后关闭读取
		 close(in,out);
	 }
 }
// private static void copyb(InputStream in,OutputStream out){
//	 try(in;out){
//		 byte[] temp=new byte[1024];
//		 int len = 0;
//		 while((len=in.read(temp))!=-1){
//			 out.write(temp,0,len);
//		 }
//		 out.flush();
//	 }catch(IOException e){
//		 e.printStackTrace();
//	 }
// }
 private static void copyc(String ins,String outs){
	 try(InputStream in=new FileInputStream(ins);OutputStream out=new FileOutputStream(outs)){//自动释放资源
		 byte[] temp=new byte[1024];
		 int len = 0;
		 while((len=in.read(temp))!=-1){
			 out.write(temp,0,len);
		 }
		 out.flush();
	 }catch(IOException e){
		 e.printStackTrace();
	 }
 }
 //资源释放
 private static void close(InputStream in,OutputStream out){
	 if(out!=null){
		 try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	 }
	 if(in!=null){
		 try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	 }
 }
 private static void close(Closeable... in){//可变参数,关闭
	 for(Closeable io:in){
		 if(io!=null){
			 try {
				io.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		 }
	 }
 }
 private static void flush(Flushable... fulsh){//可变参数,刷新
	 for(Flushable fl:fulsh){
		 if(fl!=null){
			 try {
				fl.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		 }
	 }
 }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_Pmc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值