JAVA I/O中面临字节的InputStream和OutputStream以及面向字符的Reader和Writer简介

JAVA I/O中面向字节的InputStream和OutputStream以及面向字符的Reader和Writer简介

Java的IO操作中有面向字节(Byte)和面向字符(Character)两种方式.
面向字节的操作为以8位为单位对二进制的数据进行操作,对数据不进行转换,这些类都是InputStream和OutputStream的子类.
面向字符的操作为以字符为单位对数据进行操作,在读的时候将二进制数据转为字符,在写的时候将字符转为二进制数据,这些类都是Reader和Writer的子类.
下面是JAVA示例代码:
public class ByteCharStreamTest {	
	private static void readByteByByte(BufferedInputStream bis) throws Exception{
		 int readByte = 0;
		 while((readByte = bis.read()) !=-1){
		 System.out.print(readByte + " ");
		 }
	}
	private static void readBytes(BufferedInputStream bis,int buffSize) throws Exception{
		byte[] readBytes = new byte[buffSize];
		while (true) {
			if (bis.available() >= buffSize) {
				bis.read(readBytes);
				for (byte b : readBytes) {
					System.out.print(b + " ");
				}
				System.out.println("-----one group --------");
			} else {
				readByteByByte(bis);
				break;
			}
		}
	}
	
	public static void testInPutStream() throws Exception {

		File file = new File("D:/temp/inpututf8");
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		
		readBytes(bis,10);

		bis.close();
		fis.close();
	}
	
	public static void testOutPutStream() throws Exception {

		File file = new File("D:/temp/outpututf8");
		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		bos.write("abcdefg\r\n".getBytes("UTF-8"));
		bos.write("中\r\n".getBytes("UTF-8"));
		bos.write("1234567\r\n".getBytes("UTF-8"));
		bos.write("中".getBytes("UTF-8"));

		bos.close();
		fos.close();
	}
	private static void readcharBychar(BufferedReader br ) throws Exception{
		char[] chbf = new char[10];
		
		while (br.read(chbf) != -1){//how to deal the last batch which is less than the chbf size
			for(char ch:chbf){
				System.out.print(ch +" ");
			}
			
		}
	}
	private static void readcharLines(BufferedReader br ) throws Exception{
		String line;
		while ((line=br.readLine()) != null){
			System.out.println(line);
		}
	}
	public static void testInPutReader() throws Exception {

		File file = new File("D:/temp/outputgb2312");
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader fr = new InputStreamReader(fis,"GB2312");
		BufferedReader br = new BufferedReader(fr);
		
		//readcharBychar(br);
		readcharLines(br);

		br.close();
		fr.close();
		fis.close();
	}
	
	public static void testOutputWriter() throws Exception {

		File file = new File("D:/temp/outputgb2312");
		FileOutputStream fos = new FileOutputStream(file);
		OutputStreamWriter fwr = new OutputStreamWriter(fos,"GB2312");		
		BufferedWriter bwr = new BufferedWriter(fwr);
		bwr.write("abcdefg\r\n");
		bwr.write("中\r\n");
		bwr.write("1234567\r\n");
		bwr.write("中");
		
		bwr.close();
		fwr.close();
		fos.close();		
	}
	
	
	public static void main(String[] args) throws Exception {
		testInPutStream();
		//testOutPutStream();	
		//testInPutReader();	
		//testOutputWriter();
	}
}
我们先来看对InputStream以字节流的方式处理,字节流的方式不需要指定编码,
操作的是8bit的字节.InputStream提供的read()方法返回的是这8bit代表的int值.
read(byte b[])将8bit的多个数据读入byte数组中.注意,同一个字节用int和byte表示的值是不同的.
byte的范围是-128到127.
如下面这个例子文件是UTF8的编码
abcdefg
中
1234567
中

JAVA I/O中面临字节的InputStream和OutputStream以及面向字符的Reader和Writer简介

其16进制的表示如下:a的16进制表示为61,"中"的16进制为E4 B8 AD

运行testInPutStream输出如下:
97为a的10进制表示,16进制的61和10进制的97转为二进制是相等的.
"中"的10进制为228 184 173,用byte表示为-28 -72 -83.
97 98 99 100 101 102 103 13 10 -28 -----one group --------
-72 -83 13 10 49 50 51 52 53 54 -----one group --------
55 13 10 228 184 173

InPutStream有以下的read方法.

JAVA I/O中面临字节的InputStream和OutputStream以及面向字符的Reader和Writer简介
OutPutStream输出的时候,能接收的是int,或者byte数组,然后将其以二进制的数据输出.

不要被本例的bos.write("中\r\n".getBytes("UTF-8"));所误导,这里指定编码是将"中"转成
以UTF8表示的byte数组.对同一个字符,不同的编码转换成的二进制数组可能是不一样的.
bos.write("中\r\n".getBytes("GB2312"));得到"中"的16进制表示为D6 D0.如下图
JAVA I/O中面临字节的InputStream和OutputStream以及面向字符的Reader和Writer简介
InputStreamReader需要指定编码,reader安照编码将二进制的多个字节转成一个字符.

InputStreamReader有下面的一些read方法.

JAVA I/O中面临字节的InputStream和OutputStream以及面向字符的Reader和Writer简介

BufferedReader有下面的一些read方法.
JAVA I/O中面临字节的InputStream和OutputStream以及面向字符的Reader和Writer简介
OutputStreamWriter安照指定编码将字符转成二进制数据保存.

OutputStreamWriter有下面的一些方法:

JAVA I/O中面临字节的InputStream和OutputStream以及面向字符的Reader和Writer简介