Java 输入/输出——处理流(DataInputStream/DataOutputStream、ByteArrayInputStream/ByteArrayOutputStream)
DataInputStream和DataOutputStream分别继承字节流InputStream和OutputStream,它属于处理流,需要分别“套接”在InputStream和OutputStream类型的节点流上。
DataInputStream和DataOutputStream提供了可以存取与机器无关的Java原始类型数据(如int,double等)的方法。
DataInputStream和DataOutputStream的构造方法:
- DataInputStream(InputStream in)
- DataOutputStream(OutputStream out)
ByteArrayInputStream和ByteArrayOutputStream的构造器和方法:
Constructor | Description |
---|---|
ByteArrayInputStream(byte[] buf) |
Creates a
ByteArrayInputStream so that it uses buf as its buffer array. |
ByteArrayInputStream(byte[] buf, int offset, int length) |
Creates
ByteArrayInputStream that uses buf as its buffer array. |
Modifier and Type | Method | Description |
---|---|---|
int |
available() |
Returns the number of remaining bytes that can be read (or skipped over) from this input stream.
|
void |
close() |
Closing a
ByteArrayInputStream has no effect. |
void |
mark(int readAheadLimit) |
Set the current marked position in the stream.
|
boolean |
markSupported() |
Tests if this
InputStream supports mark/reset. |
int |
read() |
Reads the next byte of data from this input stream.
|
int |
read(byte[] b, int off, int len) |
Reads up to
len bytes of data into an array of bytes from this input stream. |
void |
reset() |
Resets the buffer to the marked position.
|
long |
skip(long n) |
Skips
n bytes of input from this input stream. |
Constructor | Description |
---|---|
ByteArrayOutputStream() |
Creates a new byte array output stream.
|
ByteArrayOutputStream(int size) |
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
|
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Closing a
ByteArrayOutputStream has no effect. |
void |
reset() |
Resets the
count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. |
int |
size() |
Returns the current size of the buffer.
|
byte[] |
toByteArray() |
Creates a newly allocated byte array.
|
String |
toString() |
Converts the buffer's contents into a string decoding bytes using the platform's default character set.
|
String |
toString(int hibyte) |
Deprecated.
This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the
toString(String enc) method, which takes an encoding-name argument, or the toString() method, which uses the platform's default character encoding. |
String |
toString(String charsetName) |
Converts the buffer's contents into a string by decoding the bytes using the named
charset . |
void |
write(byte[] b, int off, int len) |
Writes
len bytes from the specified byte array starting at offset off to this byte array output stream. |
void |
write(int b) |
Writes the specified byte to this byte array output stream.
|
void |
writeTo(OutputStream out) |
Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using
out.write(buf, 0, count) . |
- package com.zyjhandsome.io;
- import java.io.*;
- public class TestDataStream {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream dos = new DataOutputStream(baos);
- try {
- dos.writeDouble(Math.random());
- dos.writeBoolean(true);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
- // bais.available()方法返回其占用的字节数目,double(8-byte)+boolean(1-byte)=9-byte
- System.out.println(bais.available());
- DataInputStream dis = new DataInputStream(bais);
- // 先存进去的是Double类型数据+Boolean类型的数据
- // 因此在读取时,也应该给先读取Double类型数据+Boolean类型的数据
- System.out.println(dis.readDouble());
- System.out.println(dis.readBoolean());
- dos.close();
- dis.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
输出结果:
- 9
- 0.03791491702144656
- true
Java 输入/输出——处理流(DataInputStream/DataOutputStream、ByteArrayInputStream/ByteArrayOutputStream)的更多相关文章
- Java 输入/输出——处理流(BufferedStream、PrintStream、转换流、推回输入流)
关于使用处理流的优势,归纳起来就是两点:(1)对于开发人员来说,使用处理流进行输入/输出操作更简单:(2)使用处理流执行效率更高. 1.BufferedInputStream/BufferedOutp ...
- Java 输入/输出——处理流(RandomAccessFile)
RandomAccessFile是Java输入/输出流体系中功能最丰富的文件内容访问类,它提供了众多的方法来访问文件内容,它既可以读取文件内容,也可以向文件输出数据.与普通的输入/输出流不同的是,Ra ...
- Java 输入/输出——处理流(ObjectIO)
Object流:直接将Object流写入或读出. TestObjectIO.java transient关键字(英文名:透明的,可以用来修饰成员变量(实例变量),transient修饰的成员变量(实例 ...
- Java 输入/输出 反射
Java 输入/输出 反射 输入输出和反射 一.数据流的基本概念 流一般分为 ( Input Stream ) 和输出流 ( Output Stream ) 两类,但这种划分并不是绝对的.比如一 ...
- Java输入/输出教程
Java输入/输出(I/O)处理从源读取数据并将数据写入目标.通常,读取存储在文件中的数据或使用I/O将数据写入到文件中. java.io和java.nio包中包含处理输入/输出的Java类.java ...
- [linux] 输入&输出&错误流
输入&输出&错误流 Linux中有三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字分别是0,1,2. 标准 数字 含义 STDIN 0 标准输入,默认从键盘读 ...
- Java输入/输出(I/O)流的分类总结
java.io中有四个重要的抽象类: InputStream(字节输入流) Reader(字符输入流) OutputStream(字节输出流) Writer(字符输出流) 其中,InputStream ...
- JAVA输入/输出系统中的其他流学习笔记
一.字节数组流 字节数组流类能够操作内存中的字节数组,它的数据是一个字节数组.字节数组流类本身适配器设计模式,它把字节数组类型转为流类型使得程序能够对字节数组进行读写操作. 1.ByteArrayIn ...
- Java 输入/输出——字节流和字符流
1.流的分类 (1)输入流和输出流(划分输入/输出流时是从程序运行所在内存的角度来考虑的) 输入流:只能从中读取数据,而不能向其写入数据. 输出流:只能向其写入数据,而不能从中读取数据. 输入流主要由 ...
随机推荐
- LaTeX语法笔记
1.单词之间用空格分隔,段落之间用一整空行分隔,但是,如果在多输入空格或者空行也没有用处,系统还是把它当做一个空格或空行. 2.双引号:左侧用``(键盘左上角那个符号),右侧用'',即: ``'' , ...
- pattern-matching as an expression without a prior match -scala
https://www.scala-lang.org/files/archive/spec/2.11/08-pattern-matching.html https://docs.scala-lang. ...
- 【iCore1S 双核心板_ARM】例程九:DAC实验——输出直流电压
实验原理: STM32内部集成12位DAC,可以配置成12位或8位,DAC具有两个独立转换通道, 在双DAC模式下,DA转换可被配置成独立模式或工作模式,iCore1S中DAC参考电压为 2.5V.本 ...
- java中的数据加密3 非对称加密
非对称加密也加公钥加密,不对称算法使用一对密钥对,一个公钥,一个私钥,使用公钥加密的数据,只有私钥能解开(可用于加密):同时,使用私钥加密的数据,只有公钥能解开(签名).但是速度很慢(比私钥加密慢10 ...
- 【转】燃烧吧,TestMice!
...当我们几个人碰面的时候,就感觉应该做点测试业内的实事. 记得当时的17站出了一些QTP辅件,给了我一些灵感.2008年做了一整年的QTP企业级实施,从方案到最后的收尾支持,得到最大的教训就是,当 ...
- Go指南练习_Reader
https://tour.go-zh.org/methods/22 一.题目描述 实现一个 Reader 类型,它产生一个 ASCII 字符 'A' 的无限流. 二.题目分析 io 包指定了 io.R ...
- 面向对象方法的重载(overloading)和覆盖(overriding)。
在有些JAVA书籍中将overriding称为重载,overloading称为过载. Overloading在一个类中可以定义多个同名方法,各个方法的参数表一定不同.但修饰词可能相同,返回值也可能 ...
- MFC AfxMessageBox(_T("Please Load Rawdata First !"));
AfxMessageBox(_T("Please Load Rawdata First !"));
- error: style attribute '@android:attr/windowEnterAnimation' not found.
在Project/gradle.properties中添加 android.enableAapt2=false
- ThreadLocal 那点事儿(续集)
本篇是<ThreadLocal 那点事儿>的续集,如果您没看上一篇,就就有点亏了.如果您错过了这一篇,那亏得就更大了. 还是保持我一贯的 Style,用一个 Demo 来说话吧.用户提出一 ...