当前位置:网站首页>实现图片的复制
实现图片的复制
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
边栏推荐
猜你喜欢

Looking for a small immutable dictionary with better performance

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)
![[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!](/img/4b/176185ba622b275404e2083df4f28a.jpg)
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!

Using fastai to develop and deploy image classifier application

MongoDB增删改查操作

The minimum insertion times of palindrome

使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性

Swagger介绍和应用

git操作与分支管理规范
随机推荐
Swagger介绍和应用
Looking for a small immutable dictionary with better performance
Creating a text cloud or label cloud in Python
AI perfume is coming. Will you buy it?
存储过程动态查询处理方法
WordPress网站程序和数据库定时备份到七牛云图文教程
都说程序员钱多空少,程序员真的忙到没时间回信息了吗?
给大家介绍下,这是我的流程图软件 —— draw.io
CMS垃圾收集器
使用Fastai开发和部署图像分类器应用
To introduce to you, this is my flow chart software—— draw.io
IT industry salary has been far ahead! Ten years later, is the programmer still a high paying profession?
Newbe.ObjectVisitor Example 1
Part I - Chapter 1 Overview
Looking for better dynamic getter and setter solutions
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
Express framework
11 important operations of Python list
net.sf.json.JSONObject对时间戳的格式化处理
接口测试工具Eolinker进行post请求