当前位置:网站首页>实现图片的复制
实现图片的复制
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
边栏推荐
- [cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note
- [elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
- Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
- Express framework
- 单例模式的五种设计方案
- CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
- abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
- net.sf.json.JSONObject对时间戳的格式化处理
- Leetcode 45 jumping game II
- To introduce to you, this is my flow chart software—— draw.io
猜你喜欢

The minimum insertion times of palindrome

Select sort

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

Express framework

Why need to use API management platform

CMS garbage collector

Learn volatile, you change your mind, I see

PAT_甲级_1056 Mice and Rice
![[cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note](/img/fe/1d868fd0087758416a6f1de6016587.jpg)
[cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note

Summary of interface test case ideas
随机推荐
abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
Brief VIM training strategy
SQL quick query
The interface testing tool eolinker makes post request
深拷贝
IT industry salary has been far ahead! Ten years later, is the programmer still a high paying profession?
都说程序员钱多空少,程序员真的忙到没时间回信息了吗?
信息安全课程设计第一周任务(7条指令的分析)
Django's simple user system (3)
选择排序
Introduction and application of swagger
Interesting article sharing: what is the difference between C language and C + +, C?
VirtualBox install centos7
IT行业薪资一直遥遥领先!十年后的程序员,是否还是一个高薪职业?
ITerm2 配置和美化
. net core cross platform resource monitoring library and dotnet tool
Part 1 - Chapter 2 pointer operation
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
latex入门
[200 interview experience], programmer interview, common interview questions analysis