Java 输入/输出——处理流(DataInputStream/DataOutputStream、ByteArrayInputStream/ByteArrayOutputStream)...

本文深入探讨了Java中DataInputStream和DataOutputStream的使用方法,包括如何处理Java原始类型数据的读写操作,以及ByteArrayInputStream和ByteArrayOutputStream的构造方法和常用API。通过示例代码展示了如何在字节数组流上进行数据的读取和写入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

  DataInputStream和DataOutputStream的构造方法:

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

  ByteArrayInputStream和ByteArrayOutputStream的构造器和方法:

ConstructorDescription
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.

 

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

 

ConstructorDescription
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.                                                                      

 

All MethodsInstance MethodsConcrete MethodsDeprecated Methods
Modifier and TypeMethodDescription
voidclose​()
Closing a  ByteArrayOutputStream has no effect.
voidreset​()
Resets the  count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.
intsize​()
Returns the current size of the buffer.
byte[]toByteArray​()
Creates a newly allocated byte array.
StringtoString​()
Converts the buffer's contents into a string decoding bytes using the platform's default character set.
StringtoString​(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.
StringtoString​(String charsetName)
Converts the buffer's contents into a string by decoding the bytes using the named  charset.
voidwrite​(byte[] b, int off, int len)
Writes  len bytes from the specified byte array starting at offset  off to this byte array output stream.
voidwrite​(int b)
Writes the specified byte to this byte array output stream.
voidwriteTo​(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).

 

 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-byte
17             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 block
27             e.printStackTrace();
28         }        
29     }
30 }

  输出结果:

1 9
2 0.03791491702144656
3 true

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值