当前位置:网站首页>实现图片的复制
实现图片的复制
2020-11-08 21:03:00 【8Years】
public class TestBytesInputStream {
public static void main(String[] args) {
//复制a.jpg图片
byte[] datas=fileToBytesArray("D:\\a.jpg");
byteArrayToFile(datas,"D:\\b.jpg");
}
//1.图片读取到字节数组中
// 1.图片到程序——FileInputStream
// 2.程序到字节数组——byteArrayOutputStream
public static byte[] fileToBytesArray(String path) {
//创建源头与目的地
File src = new File(path);
byte[] dest = null;
//选择流
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//也可以使用 is = new BufferedInputStream(FileInputStream(src));
is = new FileInputStream(src);
//也可以使用baos = new BufferedOutputStream(teArrayOutputStream());
baos = new ByteArrayOutputStream();
//3.分段读取
byte[] flush = new byte[1024 * 10];//缓冲容器
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);//写到字节数组中
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void byteArrayToFile(byte[]src,String path){
File dest=new File(path);
InputStream is=null;
OutputStream os=null;
try {
is = new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
byte[] flush = new byte[5];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(os!=null){
os.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
值得提醒的是,当is.read()没有传入参数的时候,复制的图片会无法打开,因为它只能一个字节一个字节地读,所以最好还是传入数组
提高性能的两个地方(1.缓冲容器的使用(相当于小卡车) 2.还可以使用字节缓冲流(相当于大卡车))
版权声明
本文为[8Years]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4583813/blog/4708021
边栏推荐
猜你喜欢
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
第一部分——第2章指针操作
IT行业薪资一直遥遥领先!十年后的程序员,是否还是一个高薪职业?
AI perfume is coming. Will you buy it?
简明 VIM 练级攻略
Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
Dynamic query processing method of stored procedure
Dynamic programming: maximum subarray
MongoDB数据库
[cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note
随机推荐
构造函数和原型
简明 VIM 练级攻略
C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?
构造回文的最小插入次数
The minimum insertion times of palindrome
CMS garbage collector
JVM真香系列:轻松理解class文件到虚拟机(上)
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
Chapter five
CMS垃圾收集器
Classical dynamic programming: longest common subsequence
.NET Core 跨平台资源监控库及 dotnet tool 小工具
Creating a text cloud or label cloud in Python
opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载
接口测试工具Eolinker进行post请求
如果把编程语言当武功绝学!C++是九阴真经,那程序员呢?
综合架构的简述
Using fastai to develop and deploy image classifier application