Java IO流学习总结六:ByteArrayInputStream、ByteArrayOutputStream
类的继承关系
InputStream
|__ ByteArrayInputStream
OutputStream
|__ ByteArrayOutputStream
ByteArrayInputStream 可以将字节数组转化为输入流 。
ByteArrayOutputStream 可以捕获内存缓冲区的数据,转换成字节数组。
ByteArrayInputStream
- 构造函数
public ByteArrayInputStream(byte buf[]) public ByteArrayInputStream(byte buf[], int offset, int length)
- 一般方法
void close() // 关闭该流并释放与之关联的所有资源。 String getEncoding() //返回此流使用的字符编码的名称。 int read() //读取单个字符。 int read(char[] cbuf, int offset, int length) //将字符读入数组中的某一部分。 boolean ready() //判断此流是否已经准备好用于读取。
ByteArrayOutputStream
- 构造函数
public ByteArrayOutputStream() public ByteArrayOutputStream(int size)
- 一般方法
void write(int b) void write(byte b[], int off, int len) void writeTo(OutputStream out) byte toByteArray()[] void close()
练习1
字节流 ByteArrayInputStream 的读写过程测试
package com.app;
import java.io.ByteArrayInputStream;
import java.io.IOException; public class A7 { public static void main(String[] args) { String mes = "hello,world" ;
byte[] b = mes.getBytes() ; ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( b ) ;
int result = -1 ; while( ( result = byteArrayInputStream.read() ) != -1){
System.out.println( (char) result );
} try {
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
效果:

练习2
将 ByteArrayOutputStream 读出的字节流用 FileOutputStream 写入文件
package com.app;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class A6 { public static void main(String[] args) { String mes = "你好,world" ;
byte[] b = mes.getBytes() ; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() ;
try {
byteArrayOutputStream.write( b ); FileOutputStream fileOutputStream = new FileOutputStream( new File( "F:/123.txt" ) ) ; byteArrayOutputStream.writeTo( fileOutputStream ) ; fileOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} } }
效果:

Java IO流学习总结六:ByteArrayInputStream、ByteArrayOutputStream的更多相关文章
- Java IO流学习总结(1)
Java IO流学习总结 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本 ...
- Java IO流学习总结一:输入输出流
Java IO流学习总结一:输入输出流 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/54292148 本文出自[赵彦军的博客] J ...
- Java IO流学习
Java IO流学习 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是 ...
- Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream
Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...
- Java IO流学习总结八:Commons IO 2.5-IOUtils
Java IO流学习总结八:Commons IO 2.5-IOUtils 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/550519 ...
- Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter
在上一篇文章中Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream介绍了缓冲流中的字节流,而这一篇着重介绍缓冲流中字符流Buffered ...
- JAVA.IO流学习笔记
一.java.io 的描述 通过数据流.序列化和文件系统提供系统输入和输出.IO流用来处理设备之间的数据传输 二.流 流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数 ...
- Java IO流学习总结
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- Java IO流学习总结(转)
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
随机推荐
- [UE4]AWP组合
AWP狙击枪可以由主枪和镜头模型组合而成. 一.主枪 二.镜头组合
- jstat 详解
最近项目里面使用到了多线程,有时候多线程会存在挂掉的情况,趁机好好学习总结一下JVM调优的方法. jstat使用: #jstat -help|-options #jstat -<option&g ...
- Don't afraid point
int p; int *p; int p[3]; int *p[3];分析方式:首先从P开始分析,先与[]结合因为其优先级比*高,所以p是一个数组,然后再与*结合,说明数组里的元素是指针类型,然后再与 ...
- navicat创建存储过程的小问题
再简单的东西长时间不用了就会出错,特此即时的记录下来,以便以后参考! 转自:http://blog.csdn.net/winy_lm/article/details/49690633 以下为navic ...
- 配置Jsp错误页面
配置Jsp错误页面一般我们有2种做法: (1)在页面中用指令进行配置,即page指令的errorPage和isErrorPage:可以使用page指令的errorPage来指定错误页!在当前JSP页面 ...
- Hive调优
Hive存储格式选择 和Hive 相关优化: 压缩参考 Hive支持的存储数的格式主要有:TEXTFILE .SEQUENCEFILE.ORC.PARQUET. 文件存储格式 列式存储和行式存储 行存 ...
- Elasticsearch的JavaAPI
获取客户端对象 public class App { private TransportClient client; //获取客户端对象 @Before public void getClinet() ...
- Phpstorm的强大功能
你是否也是重复,重复,一直重复的打着重复的代码? 你是否也一直重复重复 的 Ctrl+c .Ctrl+v? 当你看到此项操作的时候就会发现,原来复制粘贴还能这么玩儿... 演示效果(以phpExcel ...
- PowerDesigner 概念数据模型(CDM) 说明
ref: https://blog.csdn.net/tianlesoftware/article/details/6871179 关于PowerDesigner的说明参考: PowerDes ...
- 解决从客户端(Content="<div><p ><p>12312...")中检测到有潜在危险的Request.Form 值。
[HttpPost] [ValidateInput(false)]//解决从客户端(Content="<div><p ><p>12312..." ...