图片拷到字节数组中
图片(文件)不能直接转到字节数组中,需要一个程序做个中转。
图片(文件)----文件字节输入流---->程序----字节数组输出流---->字节数组
反过来就是:字节数组----字节数组输入流---->程序----文件字节输出流----图片(文件)
都是以程序为中心
图片读取到字节数组、字节数组写出到文件
package com.sxt.io;
import java.io.*;
/**
* 1、图片读取到字节数组
* 2、字节数组写出到文件
*/
public class IOTest09 {
public static void main(String[] args) {
//图片转成字节数组
byte[] datas = fileToByteArray("p.png");
System.out.println(datas.length);
byteArrayToFile(datas, "p-byte.png");
}
/**
* 1、图片读取到字节数组
* 1)图片到程序:FileInputStream
* 2)程序到字节数组 ByteArrayOutputStream
*/
public static byte[] fileToByteArray(String filePath) {
//创建源与目的地
File src = new File(filePath);
byte[] dest = null;
//选择流
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
is = new FileInputStream(src);
baos = new ByteArrayOutputStream();
//操作(分段读取)
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 {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 1、字节数组写出到图片
* 1)字节数组读取到程序:ByteArrayInputStream
* 2)程序到文件: OutputStream
*/
public static void byteArrayToFile(byte[] src, String filePath) {
//创建源
File dest = new File(filePath);
//选择流
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);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
封装拷贝、封装释放资源
package com.sxt.io;
import java.io.*;
/**
* 1、封装拷贝
* 2、封装释放资源
*/
public class FileUtils {
public static void main(String[] args) {
//文件到文件
try {
InputStream is=new FileInputStream("abc.txt");
OutputStream os=new FileOutputStream("abc-copy.txt");
copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}
//文件到字节数组
//文件到文件
byte[] datas=null;
try {
InputStream is=new FileInputStream("p.png");
ByteArrayOutputStream baos=new ByteArrayOutputStream ();
copy(is,baos);
datas=baos.toByteArray();
System.out.println(datas.length);
} catch (IOException e) {
e.printStackTrace();
}
//字节数组到文件
try {
InputStream is= new ByteArrayInputStream(datas);
OutputStream os=new FileOutputStream("p-copy.png");
copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 对接输入输出流
* @param is
* @param os
*/
public static void copy(InputStream is,OutputStream os){
try {
//操作(分段读取)
byte[] flush =new byte[1024];//缓冲容器
int len=-1;//接受长度
while ((len = is.read(flush)) != -1){
//分段写出
os.write(flush,0,len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4、释放资源 分别关闭 先打开的后关闭
close(is,os );
}
}
/**
* 释放资源
* @param is
* @param os
*/
public static void close(InputStream is,OutputStream os){
try {
if (null!=os)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (null!=is)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 释放资源
* @param ios
*/
public static void close(Closeable...ios){
for (Closeable io:ios) {
try {
if (null != io)
io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
try…with…resource释放资源
package com.sxt.io;
import java.io.*;
/**
* 1、封装拷贝
* 2、封装释放资源
*/
public class FileUtils02 {
public static void main(String[] args) {
//文件到文件
try {
InputStream is=new FileInputStream("abc.txt");
OutputStream os=new FileOutputStream("abc-copy.txt");
copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}
//文件到字节数组
//文件到文件
byte[] datas=null;
try {
InputStream is=new FileInputStream("p.png");
ByteArrayOutputStream baos=new ByteArrayOutputStream ();
copy(is,baos);
datas=baos.toByteArray();
System.out.println(datas.length);
} catch (IOException e) {
e.printStackTrace();
}
//字节数组到文件
try {
InputStream is= new ByteArrayInputStream(datas);
OutputStream os=new FileOutputStream("p-copy.png");
copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 对接输入输出流
* try...with...resource
* @param is
* @param os
*/
public static void copy(InputStream is,OutputStream os){
try(is;os) {
//操作(分段读取)
byte[] flush =new byte[1024];//缓冲容器
int len=-1;//接受长度
while ((len = is.read(flush)) != -1){
//分段写出
os.write(flush,0,len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
处理流,进行装饰
实现放大器对声音的放大功能
package com.sxt.io;
/**
* 实现放大器对声音的放大功能
*/
public class DecorateTest01 {
public static void main(String[] args) {
Person p=new Person();
p.say();
//装饰 放大
Amplifier am =new Amplifier(p);
am.say();
}
interface Say{
void say();
}
static class Person implements Say{
//属性
private int voice=70;
@Override
public void say() {
System.out.println("人的声音为:"+voice);
}
public int getVoice() {
return voice;
}
public void setVoice(int voice) {
this.voice = voice;
}
}
//放大器
static class Amplifier implements Say{
private Person p;
Amplifier(Person p){
this.p=p;
}
public void say(){
System.out.println("人的声音为:"+p.getVoice()*100);
System.out.println("噪音");
}
}
}
模拟咖啡
1、抽象组件:需要装饰的抽象对象(接口或抽象父类)
2、具体的组件:需要装饰的对象
3、抽象装饰类:包含了对抽象组件的引用以及装饰着共有的方法
4、装饰类:被装饰的对象
package com.sxt.io;
import javax.imageio.plugins.jpeg.JPEGImageReadParam;
/**
* 模拟咖啡
* 1、抽象组件:需要装饰的抽象对象(接口或抽象父类)
* 2、具体的组件:需要装饰的对象
* 3、抽象装饰类:包含了对抽象组件的引用以及装饰着共有的方法
* 4、装饰类:被装饰的对象
*/
public class DecorateTest02 {
public static void main(String[] args) {
Drink coffee=new Coffee();
Drink suger=new Suger(coffee);//装饰
System.out.println(suger.info()+"----------->"+suger.cost());
Drink milk=new Milk(coffee);//装饰
System.out.println(milk.info()+"----------->"+milk.cost());
milk=new Milk(suger);//装饰
System.out.println(milk.info()+"----------->"+milk.cost());
suger=new Suger(milk);//装饰
System.out.println(suger.info()+"----------->"+suger.cost());
}
}
interface Drink{
double cost();//费用
String info();//说明
}
//具体组件
class Coffee implements Drink{
private String name="原味咖啡";
@Override
public double cost() {
return 10;
}
@Override
public String info() {
return name;
}
}
//抽象装饰类
abstract class Decorate implements Drink{
//对抽象组件的引用
private Drink drink;
Decorate(Drink drink){
this.drink=drink;
}
@Override
public double cost() {
return this.drink.cost();
}
@Override
public String info() {
return this.drink.info();
}
}
//具体的装饰类
class Milk extends Decorate{
@Override
public double cost() {
return super.cost()*4;
}
@Override
public String info() {
return super.info()+"加入了牛奶";
}
public Milk(Drink drink) {
super(drink);
}
}
class Suger extends Decorate{
public Suger(Drink drink) {
super(drink);
}
@Override
public double cost() {
return super.cost()*2;
}
@Override
public String info() {
return super.info()+"加入了蔗糖";
}
}