类的继承关系

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的更多相关文章

  1. Java IO流学习总结(1)

    Java IO流学习总结 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本 ...

  2. Java IO流学习总结一:输入输出流

    Java IO流学习总结一:输入输出流 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/54292148 本文出自[赵彦军的博客] J ...

  3. Java IO流学习

    Java IO流学习 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是 ...

  4. Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream

    Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...

  5. Java IO流学习总结八:Commons IO 2.5-IOUtils

    Java IO流学习总结八:Commons IO 2.5-IOUtils 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/550519 ...

  6. Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter

    在上一篇文章中Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream介绍了缓冲流中的字节流,而这一篇着重介绍缓冲流中字符流Buffered ...

  7. JAVA.IO流学习笔记

    一.java.io 的描述 通过数据流.序列化和文件系统提供系统输入和输出.IO流用来处理设备之间的数据传输 二.流 流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数 ...

  8. Java IO流学习总结

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  9. Java IO流学习总结(转)

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

随机推荐

  1. [UE4]自定义函数,快速增加输入参数的一种方法

  2. Linux开机自动挂载windows网络共享

    yum install samba-client yum install cifs.utils yum install samba-common 命令: mount -v -t  cifs    // ...

  3. 解决在word中不能使用输入法

    打开一个Word文档-------------->单击 文件---->选项---->高级---->取消什么?看图 然后重新启动word

  4. 在线学习和在线凸优化(online learning and online convex optimization)—基础介绍1

    开启一个在线学习和在线凸优化框架专题学习: 1.首先介绍在线学习的相关概念 在线学习是在一系列连续的回合(rounds)中进行的: 在回合,学习机(learner)被给一个question:(一个向量 ...

  5. mac一些设置

    Mac自带了的JDK6,安装在目录:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/下. JDK8则需要自己到Oracle官网下载安装对应的版本. ...

  6. Mysql-Client编码问题

    Mysql编码问题! 首先,安装完mysql之后,登录进去(从控制台), 先要查看mysql编码:SHOW VARIABLES LIKE 'char%': (我这里是修改好的) 如果是没修改的,因为当 ...

  7. 流(Stream)与文件流(FileStream)

    //通过流的方式添加 StreamWriter writer = new StreamWriter(@"C:\A\ca.txt", true, Encoding.Default); ...

  8. Spring MVC开启注解

    1.在spring的配置文件中配置:<context:annotation-config />该项配置只能应用于已经注册的bean,比较难用,不深究. 2.在spring的配置文件中使用c ...

  9. Spring获取application.properties

    方法一:@Value获取属性值 首先在application.properties中添加属性值 app.name=MyApp app.description=${app.name} is a Spri ...

  10. 压缩(zip)

    默认情况这些压缩工具在压缩后会删除源文件(zip除外):而且默认只压缩文件,而不压缩目录(链接到打包程序). gzip bzip2 zip GNU tar 1.gzip 1.1.压缩 gzip 压缩工 ...