当前位置:网站首页>Copy the picture
Copy the picture
2020-11-08 21:03:00 【8Years】
public class TestBytesInputStream {
public static void main(String[] args) {
// Copy a.jpg picture
byte[] datas=fileToBytesArray("D:\\a.jpg");
byteArrayToFile(datas,"D:\\b.jpg");
}
//1. The image is read into the byte array
// 1. Picture to program ——FileInputStream
// 2. Program to byte array ——byteArrayOutputStream
public static byte[] fileToBytesArray(String path) {
// Create source and destination
File src = new File(path);
byte[] dest = null;
// Select flow
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// You can also use is = new BufferedInputStream(FileInputStream(src));
is = new FileInputStream(src);
// You can also use baos = new BufferedOutputStream(teArrayOutputStream());
baos = new ByteArrayOutputStream();
//3. Segment read
byte[] flush = new byte[1024 * 10];// Buffer container
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);// Write to a byte array
}
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();
}
}
}
}
It's worth reminding , When is.read() When no parameters are passed in , The copied image will not open , Because it can only read byte by byte , So it's better to pass in the array
Two places to improve performance (1. The use of buffer containers ( It's like a pickup truck ) 2. You can also use byte buffer streams ( It's like a big truck ))
版权声明
本文为[8Years]所创,转载请带上原文链接,感谢
边栏推荐
- Using annotation + interceptor to implement asynchronous execution
- Regular backup of WordPress website program and database to qiniu cloud
- Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
- 综合架构的简述
- The minimum insertion times of palindrome
- CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
- 文件拷贝的实现
- 程序员都应该知道的URI,一文帮你全面了解
- 动态规划设计:最大子数组
- Dynamic planning
猜你喜欢

Opencv solves the problem of ippicv download failure_ 2019_ lnx_ intel64_ general_ 20180723.tgz offline Download

C/C++学习日记:原码、反码和补码

API生命周期的5个阶段

Flink series (0) -- Preparation (basic stream processing)

CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?

C/C++知识分享: 函数指针与指针函数,看完这篇你还能不懂?

Why need to use API management platform

进程 线程 协程

在Python中创建文字云或标签云

为什么需要使用API管理平台
随机推荐
在Python中创建文字云或标签云
接口测试工具Eolinker进行post请求
装饰器(二)
Express framework
200 programmers interview experience, all here
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
如何将PyTorch Lightning模型部署到生产中
Programmers should know the URI, a comprehensive understanding of the article
C/C++知识分享: 函数指针与指针函数,看完这篇你还能不懂?
单例模式的五种设计方案
MongoDB数据库
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
Case analysis of entitycore framework
Django's simple user system (3)
解决go get下载包失败问题
Iptables from introduction to mastery
国内三大云数据库测试对比
C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?
第一部分——第2章指针操作