Byte stream copy pictures

Text, images, video is written, only the source and destination data need to be clear on it
a, reads one byte

//封装数据源
		FileInputStream fis=new FileInputStream("C:\\Users\\29265\\Desktop\\4\\1.png");
		//封装目的地
		FileOutputStream fos=new FileOutputStream("C:\\Users\\29265\\Desktop\\2.png");
		
		int by;
		while((by=fis.read())!=-1){
			fos.write(by);//每次读取一个字节写入
		}
		
		//释放资源,顺序无关
		fis.close();
		fos.close();

Second, a byte array is read once
this method is faster

//封装数据源
		FileInputStream fis=new FileInputStream("C:\\Users\\29265\\Desktop\\4\\1.png");
		//封装目的地
		FileOutputStream fos=new FileOutputStream("C:\\Users\\29265\\Desktop\\3.png");
		
		//数组的大小一般是1024的倍数
		byte[] bs=new byte[1024];
		int len;
		//fis.read(bs)如果找到数据就会返回-1
		while((len=fis.read(bs))!=-1){
			fos.write(bs,0,len);
		}
		
		//释放资源,顺序无关
		fis.close();
		fos.close();
Published 210 original articles · won praise 10 · views 10000 +

Guess you like

Origin blog.csdn.net/Ting1king/article/details/105009453