博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 输入/输出——处理流(DataInputStream/DataOutputStream、ByteArrayInputStream/ByteArrayOutputStream)...
阅读量:5270 次
发布时间:2019-06-14

本文共 3915 字,大约阅读时间需要 13 分钟。

  DataInputStream和DataOutputStream分别继承字节流InputStream和OutputStream,它属于处理流,需要分别“套接”在InputStream和OutputStream类型的节点流上。

  DataInputStream和DataOutputStream提供了可以存取与机器无关的Java原始类型数据(如int,double等)的方法。

  DataInputStream和DataOutputStream的构造方法:

  • DataInputStream(InputStream in)
  • DataOutputStream(OutputStream out)

  ByteArrayInputStream和ByteArrayOutputStream的构造器和方法:

Constructor Description
​(byte[] buf)
Creates a 
ByteArrayInputStream so that it uses 
buf as its buffer array.                                                             
​(byte[] buf, int offset, int length)
Creates 
ByteArrayInputStream that uses 
buf as its buffer array.

 

All MethodsInstance MethodsConcrete Methods
Modifier and Type Method Description
int ​()
Returns the number of remaining bytes that can be read (or skipped over) from this input stream.                  
void ​()
Closing a 
ByteArrayInputStream has no effect.
void ​(int readAheadLimit)
Set the current marked position in the stream.
boolean ​()
Tests if this 
InputStream supports mark/reset.
int ​()
Reads the next byte of data from this input stream.
int ​(byte[] b, int off, int len)
Reads up to 
len bytes of data into an array of bytes from this input stream.
void ​()
Resets the buffer to the marked position.
long ​(long n)
Skips 
n bytes of input from this input stream.

 

Constructor Description
​()
Creates a new byte array output stream.
​(int size)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.                                                                      

 

All MethodsInstance MethodsConcrete MethodsDeprecated Methods
Modifier and Type Method Description
void ​()
Closing a 
ByteArrayOutputStream has no effect.
void ​()
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 ​()
Returns the current size of the buffer.
byte[] ​()
Creates a newly allocated byte array.
​()
Converts the buffer's contents into a string decoding bytes using the platform's default character set.
​(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.
​( charsetName)
Converts the buffer's contents into a string by decoding the bytes using the named  .
void ​(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 ​(int b)
Writes the specified byte to this byte array output stream.
void ​( 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).

 

1 package com.zyjhandsome.io; 2  3 import java.io.*; 4  5 public class TestDataStream { 6  7     public static void main(String[] args) { 8         // TODO Auto-generated method stub 9         ByteArrayOutputStream baos = new ByteArrayOutputStream();10         DataOutputStream dos = new DataOutputStream(baos);11         12         try {13             dos.writeDouble(Math.random());14             dos.writeBoolean(true);15             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());16             // bais.available()方法返回其占用的字节数目,double(8-byte)+boolean(1-byte)=9-byte17             System.out.println(bais.available());18             DataInputStream dis = new DataInputStream(bais);19             // 先存进去的是Double类型数据+Boolean类型的数据20             // 因此在读取时,也应该给先读取Double类型数据+Boolean类型的数据21             System.out.println(dis.readDouble());22             System.out.println(dis.readBoolean());23             dos.close();24             dis.close();            25         } catch (IOException e) {26             // TODO Auto-generated catch block27             e.printStackTrace();28         }        29     }30 }

  输出结果:

1 92 0.037914917021446563 true

 

转载于:https://www.cnblogs.com/zyjhandsome/p/9698036.html

你可能感兴趣的文章
iOS-程序启动原理和UIApplication
查看>>
mysql 8.0 zip包安装
查看>>
awk 统计
查看>>
CSS min-height 属性
查看>>
模板设计模式的应用
查看>>
实训第五天
查看>>
平台维护流程
查看>>
2012暑期川西旅游之总结
查看>>
Linux发行版的排行
查看>>
12010 解密QQ号(队列)
查看>>
2014年辛星完全解读Javascript第一节
查看>>
装配SpringBean(一)--依赖注入
查看>>
daydayup2 codeforces143C
查看>>
ANT打包J2EE项目war包
查看>>
UESTC-我要长高 DP优化
查看>>
java选择文件时提供图像缩略图[转]
查看>>
方维分享系统二次开发, 给评论、主题、回复、活动 加审核的功能
查看>>
Matlab parfor-loop并行运算
查看>>
string与stringbuilder的区别
查看>>
2012-01-12 16:01 hibernate注解以及简单实例
查看>>