当前位置:网站首页>实现图片的复制
实现图片的复制
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
边栏推荐
- Brief introduction of Integrated Architecture
- Iptables from introduction to mastery
- Suffix expression to infix expression
- [cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note
- Newbe.ObjectVisitor 样例 1
- To introduce to you, this is my flow chart software—— draw.io
- 趣文分享:C 语言和 C++、C# 的区别在什么地方?
- 新手入坑指南:工作原因“重启”Deepin系统,发现真的香啊
- IT行业薪资一直遥遥领先!十年后的程序员,是否还是一个高薪职业?
- Looking for better dynamic getter and setter solutions
猜你喜欢

Using annotation + interceptor to implement asynchronous execution

.NET Core 跨平台资源监控库及 dotnet tool 小工具

Part 1 - Chapter 2 pointer operation

Regular backup of WordPress website program and database to qiniu cloud

Introduction to latex

Using fastai to develop and deploy image classifier application

MongoDB数据库

VirtualBox安装centos7

Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?

构造函数和原型
随机推荐
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
简明 VIM 练级攻略
RSA非对称加密算法
Part I - Chapter 1 Overview
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
经典动态规划:最长公共子序列
Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
. net core cross platform resource monitoring library and dotnet tool
Express框架
如何将PyTorch Lightning模型部署到生产中
An online accident caused by improper use of thread pool
EntityFramework Core上下文实例池原理分析
Mycat搭建
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
第一部分——第2章指针操作
Wechat applet related
200 programmers interview experience, all here
The interface testing tool eolinker makes post request
Swagger介绍和应用
不是程序员,代码也不能太丑!python官方书写规范:任何人都该了解的 pep8