您的位置:首页 > 大数据 > 人工智能

数据流DataInputStream和内存流ByteArrayInputStream

2016-08-23 17:40 429 查看
数据输入流和数据输出流

提供了各种方法方便数据的输入和输出

public static void main(String[] args) throws IOException
{
writeData();
readData();
}
public static void readData()throws IOException
{
DataInputStream dis =new DataInputStream(new FileInputStream("data.txt"));

System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
System.out.println(dis.readChar());
dis.close();
}

public static void writeData()throws IOException
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));

dos.writeDouble(66.78);
dos.writeBoolean(false);
dos.writeChar('k');

dos.close();
}


内存输入流和内存输出流

内存输入流把内存中的数据存入缓冲区

public static void main(String[] args) throws IOException
{
//内存流:
//ByteArrayInputStream:从内存中读,
//ByteArrayInputStream(byte[] buf)//从字节数组中读

//ByteArrayOutputStream:向内存中写
//内部有一个字节数组,数据被写到该数组中

byte[] arr = "hello,大家好!".getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(arr);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] a = new byte[1024];
int len = 0;
while((len = bis.read(a))!=-1)
{
bos.write(a,0,len);
}
//得到ByteArrayOutputStream对象内部数组中的数据
//将对象内部数组中的数据写入data
byte[] data = bos.toByteArray();
System.out.println(new String(data));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据流 内存流
相关文章推荐