优势 及要点
1.任何数据都可以处理成二进制数组, 方便后期网络上的传输 。
2.源头是一块内存而不是文件,java虚拟机可以直接访问
3.因为源头是内存,所以数据不能太大
4不用释放,java虚拟机会自动释放。
字节数组输入流
package 字节数组流;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class 字节数组读取流 {
public static void main(String[] args) {
//1.选择源
byte [] a= "talk the cheap show me the code".getBytes();
//2.选择流
InputStream in = null;
in = new ByteArrayInputStream(a);
//3.操作
byte [] flush = new byte [128];
int len =-1;
try {
while((len=in.read(flush))!=-1) {
String str =new String(flush,0,flush.length);
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//4.释放资源 为了形式统一,可以不用
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字节数组输出流
package 字节数组流;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class 字节数组输出流 {
public static void main(String[] args) {
//选择源
byte [] file =null;//不需要源
//选择流 (因为子类有新的方法 所以不能再用父类)
ByteArrayOutputStream output =null;
try {
output = new ByteArrayOutputStream();
//操作
String s ="talk is cheap show me the code";
byte [] n=s.getBytes();//编码
output.write(n, 0, n.length);
output.flush();//刷新
//获取数据
file=output.toByteArray();
System.out.println(new String(file ,0 ,file.length));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//关闭流
if(null!=output) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
综合对接流
package 综合起来的;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class 综合_对接流 {//文件——程序——字节数组 (文件到字节数组)
//字节数组——程序——文件(字节数组到文件)
public static void main(String[] args) {
arrayfile(filearray("1.jpg"), "2.jpg");
}
public static byte[] filearray(String path) {//文件到字节数组
File file = new File(path);
byte [] date =null;
//选择流
InputStream in= null;
ByteArrayOutputStream inarray= null;
try {
in = new FileInputStream(file);
inarray =new ByteArrayOutputStream();
//操作(分段读取)
byte [] flush =new byte [128];//缓冲容器
int len = -1;//接收容器
while((len = in.read(flush))!=-1) {
inarray.write(flush,0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//释放资源
if(null!=in) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return inarray.toByteArray();//将字节数组返回
}
public static void arrayfile(byte []array,String path) {//字节数组到文件
//选择源
File file =new File(path);
//选择流
OutputStream output =null;
InputStream arrayin = null;
try {
output = new FileOutputStream(file );
//操作
arrayin =new ByteArrayInputStream(array);
output.write(array, 0, array.length);
output.flush();//刷新
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//关闭流
if(null!=output) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}