当前位置:网站首页>实现图片的复制
实现图片的复制
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
边栏推荐
- Dynamic planning
- Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
- Newbe.ObjectVisitor 样例 1
- Package subsystem in Simulink
- Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan
- Chapter five
- CMS garbage collector
- 使用Fastai开发和部署图像分类器应用
- 使用Fastai开发和部署图像分类器应用
- go语言参数传递到底是传值还是传引用?
猜你喜欢

opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载

npm install 无响应解决方案

Mycat搭建

使用Fastai开发和部署图像分类器应用

接口测试用例思路总结

Proficient in high concurrency and multithreading, but can't use ThreadLocal?

Summary of interface test case ideas

构造回文的最小插入次数

都说程序员钱多空少,程序员真的忙到没时间回信息了吗?

11 important operations of Python list
随机推荐
Creating a text cloud or label cloud in Python
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
SQL quick query
abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
如果把编程语言当武功绝学!C++是九阴真经,那程序员呢?
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
Mongodb database
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
【Elasticsearch 技术分享】—— 十张图带大家看懂 ES 原理 !明白为什么说:ES 是准实时的!
单例模式的五种设计方案
PAT_甲级_1056 Mice and Rice
综合架构的简述
Using GaN based oversampling technique to improve the accuracy of model for mortality prediction of unbalanced covid-19
Constructors and prototypes
VirtualBox安装centos7
给大家介绍下,这是我的流程图软件 —— draw.io
Classical dynamic programming: longest common subsequence
第五章
Newbe.ObjectVisitor 样例 1
后缀表达式转中缀表达式