package 處理流;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Demo02 {
public static void main(String[] args) throws IOException{
read(write());
}
//把數據+類型寫到字節數組裡
public static byte[] write()throws IOException
{
byte[] b2=null;
ByteArrayOutputStream dos=new ByteArrayOutputStream();
DataOutputStream out=new DataOutputStream(
new BufferedOutputStream(dos));
double point=3.14159;
int num=2628;
String str="我是汪洋";
String str02="劉林是我同學";
out.writeDouble(point);
out.writeInt(num);
out.writeUTF(str);
out.writeUTF(str02);
dos.flush();
b2=dos.toByteArray();
return b2;
}
//從字節數組中獲得數據+類型
public static void read(byte[] b2)throws IOException
{
DataInputStream into=new DataInputStream(
new BufferedInputStream(
new ByteArrayInputStream(b2)));
System.out.println("point="+into.readDouble());
System.out.println("num="+into.readInt());
System.out.println("str="+into.readUTF());
System.out.println("str02="+into.readUTF());
}
}
out.writeUTF(str02);
out.flush();//加上這句,把流輸出到dos
dos.flush();