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.

 

All MethodsInstance MethodsConcrete Methods
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.                                                                      

 

All MethodsInstance MethodsConcrete MethodsDeprecated Methods
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).

 

 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM