文字、图片、视频都是这样写的,只需要明确数据源和目的地就可以了
一、一次读取一个字节
//封装数据源
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();
二、一次读取一个字节数组
这种方法速度更快
//封装数据源
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();