在劫

吾生也有涯,而知也无涯 。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

ByteArrayInputStream和ByteArrayOutputStream###

  ByteArrayOutputStream在创建实例时,程序内部创建一个byte型数组的缓冲区,然后利用By特ArrayOutputStream和ByteArrayInputStream的实例向数组写入或读出byte型数据。在网络传输中我们往往要传输很多变量。可以利用ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去。
  ByteArrayOutputStream把内存中的数据读到字节数组中,而ByteArrayInputStream又把数组中的字节数以流的形式读出,实现对统一字节数组的操作。综合DataOutputStream和DataInputStream的作用与ByteArrayInputStream和ByteArrayOutputStream使用更方便。
  ByteArrayInputStream

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamTest {
	public static void main(String[] args) throws IOException {
		String str = "hello world";
		byte[] b = str.getBytes();

		//ByteArrayInputStream(byte[] buf) //使用创建者预设的buf作为其缓冲区数组,通常buf就是数据源
		//ByteArrayInputStream(byte[] buf, int offset, int lenght) //使用buf作为其缓冲区数组,参数offset指定从数组中开始读取数据的起始下标位置,lenght指定从数组中读取的字节数。
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		int i = 0;
		//读方式一:
//		while((i = bais.read()) != -1) {
//			System.out.print((char)i);
//		}
		
		//读方式二:
		byte[] buf = new byte[1024];
		while((i = bais.read(buf)) != -1) {
			System.out.println(new String(buf, 0, i));
		}
		//close()方法无法关闭ByteArrayInputStream
		bais.close();
	}
}

  ByteArrayOutputStream

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteOutputStream {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String str = "hello world java";
		byte[] b = str.getBytes();
		File f = new File("d:\\1024.txt");
		FileOutputStream fos = new FileOutputStream(f);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//写出方式一:
//		//读取输出流
//		baos.write(b);
//		//将byte数组输出流中的所有数据写到指定的输出流中
//		baos.writeTo(fos);
		
		//写出方式二:
		baos.write(b);	
		baos.writeTo(fos);
		
		//可以通过toString()方法将输出流转换为字符串
		System.out.println(baos.toString());
		
		fos.close();
		//close()方式无效
		baos.close();
	}

}

DataInputStream和DataOutputStream###

  DataInputStream

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class DataInputStreamTest {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String strFile = "d:" + File.separator + "DataOutputStream.txt";
		File f = new File(strFile);
		DataInputStream dis = new DataInputStream(new FileInputStream(f));
		byte[] b = new byte[1024];
		int buf = 0;
		while(-1 != (buf = (dis.read(b)))) {
			System.out.print(new String(b, 0, buf));
		}
	}

}

  DataOutputStream

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamTest {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String strFile = "d:" + File.separator + "DataOutputStream.txt";
		File f = new File(strFile);
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
		String str = "hello world java";
		dos.writeBytes(str);
		//DataOutputStream要和DataInputStream配合使用
		//JAVA中的char是16位的,一个char存储一个中文字符,直接用writeBytes方法转换会变为8位,直接导致高8位丢失。从而导致中文乱码。
		//所以下面先字符串先转换成byte数组,中文就不会出现乱码
		dos.write("写入记事本内容".getBytes());
		//直接写入中文,乱码
		dos.writeBytes("写入记事本内容");
		dos.writeBytes("写入记事本内容");
		dos.writeBytes("写入记事本内容");
		dos.writeBytes("写入记事本内容");
		dos.flush();
		dos.close();
	}
}
posted on 2018-03-13 11:04  长嘴大耳怪  阅读(124)  评论(0编辑  收藏  举报