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

Java的IO操作中有面向字节(Byte)和面向字符(Character)两种方式.
面向字节的操作为以8位为单位对二进制的数据进行操作,对数据不进行转换,这些类都是InputStream和OutputStream的子类.
面向字符的操作为以字符为单位对数据进行操作,在读的时候将二进制数据转为字符,在写的时候将字符转为二进制数据,这些类都是Reader和Writer的子类.
下面是JAVA示例代码:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ByteCharStreamTest {      
  2.     private static void readBytes(BufferedInputStream bis,int buffSize) throws Exception{  
  3.         byte[] readBytes = new byte[buffSize];  
  4.           
  5.         int len;  
  6.         while((len=bis.read(readBytes)) !=-1){  
  7.             for(int i=0; i<len;i++){  
  8.                 System.out.print(readBytes[i] + " ");  
  9.             }  
  10.             System.out.println("-----one group --------");  
  11.         }  
  12.     }  
  13.       
  14.     public static void testInPutStream(String fileName) throws Exception {  
  15.   
  16.         File file = new File(fileName);  
  17.         FileInputStream fis = new FileInputStream(file);  
  18.         BufferedInputStream bis = new BufferedInputStream(fis);  
  19.           
  20.         readBytes(bis,10);  
  21.   
  22.         bis.close();  
  23.         fis.close();  
  24.     }  
  25.       
  26.     public static void testOutPutStream() throws Exception {  
  27.   
  28.         File file = new File("D:/temp/outpututf8");  
  29.         FileOutputStream fos = new FileOutputStream(file);  
  30.         BufferedOutputStream bos = new BufferedOutputStream(fos);  
  31.           
  32.         bos.write("abcdefg\r\n".getBytes("UTF-8"));  
  33.         bos.write("中\r\n".getBytes("UTF-8"));  
  34.         bos.write("1234567\r\n".getBytes("UTF-8"));  
  35.         bos.write("中".getBytes("UTF-8"));  
  36.   
  37.         bos.close();  
  38.         fos.close();  
  39.     }  
  40.     private static void readcharBychar(BufferedReader br ) throws Exception{  
  41.         char[] chbf = new char[10];  
  42.         int len;  
  43.         while ((len=br.read(chbf) )!= -1){  
  44.             for(int i=0;i<len;i++){  
  45.                 System.out.print(chbf[i]);  
  46.             }  
  47.               
  48.         }  
  49.     }  
  50.     private static void readcharLines(BufferedReader br ) throws Exception{  
  51.         String line;  
  52.         while ((line=br.readLine()) != null){  
  53.             System.out.println(line);  
  54.         }  
  55.     }  
  56.     public static void testInPutReader(String fileName,String charset) throws Exception {  
  57.   
  58.         File file = new File(fileName);  
  59.         FileInputStream fis = new FileInputStream(file);  
  60.         InputStreamReader fr = new InputStreamReader(fis,charset);  
  61.         BufferedReader br = new BufferedReader(fr);          
  62.         readcharBychar(br);  
  63.         //readcharLines(br);  
  64.         br.close();  
  65.         fr.close();  
  66.         fis.close();  
  67.     }  
  68.       
  69.     public static void testOutputWriter() throws Exception {  
  70.   
  71.         File file = new File("D:/temp/outputgb2312");  
  72.         FileOutputStream fos = new FileOutputStream(file);  
  73.         OutputStreamWriter fwr = new OutputStreamWriter(fos,"GB2312");          
  74.         BufferedWriter bwr = new BufferedWriter(fwr);  
  75.         bwr.write("abcdefg\r\n");  
  76.         bwr.write("中\r\n");  
  77.         bwr.write("1234567\r\n");  
  78.         bwr.write("中");  
  79.         bwr.close();  
  80.         fwr.close();  
  81.         fos.close();          
  82.     }  
  83.       
  84.     public static void main(String[] args) throws Exception {  
  85.         testInPutStream("C:/D/charset/gb2312.txt");  
  86.         //testOutPutStream();      
  87.         //testInPutReader();  
  88.         //testInPutReader("C:/D/charset/gb2312.txt","gb2312");  
  89.         //testOutputWriter();  
  90.         //testStreamSource();  
  91.     }  
  92. }  
我们先来看对InputStream以字节流的方式处理,字节流的方式不需要指定编码,
操作的是8bit的字节.InputStream提供的read()方法返回的是这8bit代表的int值.
read(byte b[])将8bit的多个数据读入byte数组中.注意,同一个字节用int和byte表示的值是不同的.
byte的范围是-128到127,超过127的用负数表示.
如下面这个例子文件是UTF8的编码
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. abcdefg  
  2. 中  
  3. 1234567  
  4. 中  


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

运行testInPutStream输出如下:
97为a的10进制表示,16进制的61和10进制的97转为二进制是相等的.
"中"的10进制为228 184 173,用byte表示为-28 -72 -83.
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 97 98 99 100 101 102 103 13 10 -28 -----one group --------  
  2. -72 -83 13 10 49 50 51 52 53 54 -----one group --------  
  3. 55 13 10 228 184 173  

InPutStream有以下的read方法.


OutPutStream输出的时候,能接收的是int,或者byte数组,然后将其以二进制的数据输出.

不要被本例的bos.write("中\r\n".getBytes("UTF-8"));所误导,这里指定编码是将"中"转成 
以UTF8表示的byte数组.对同一个字符,不同的编码转换成的二进制数组可能是不一样的.
bos.write("中\r\n".getBytes("GB2312"));得到"中"的16进制表示为D6 D0.如下图,"中"在utf8中占用3个字节,在gb2312中占有两个字节.

InputStreamReader需要指定编码,reader安照编码将二进制的多个字节转成一个字符.

InputStreamReader有下面的一些read方法.


BufferedReader有下面的一些read方法.有支持按行读取的方法。

OutputStreamWriter安照指定编码将字符转成二进制数据保存.

OutputStreamWriter有下面的一些方法:


做JAVA IO操作的时候,我们通常都是将多个对象重叠在一起,提供自己期望的功能的.
通常是在Stream外套上一个Buffered类来提高IO效率和提供更方便的操作方法.
InputStream的作用是标志那些从不同起源地产生输入的类.这些起源地包括(每个都有一个相关的InputStream子类):
(1)字节数组:ByteArrayInputStream(byte buf[])
(2)String对象:StringBufferInputStream(String s)
(3)文件:FileInputStream(file)
(4)其他起源,如InputStream socketIn = socket.getInputStream();
OutputStream的作用是标志输出到不同的目的地.这些目的地包括(每个都有一个相关的OutputStream子类):
(1)字节数组:ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayOutputStream在内存中创建一个缓冲区.我们发送给流的所有数据都会置入这个缓冲区.
(2)文件:FileOutputStream(file)
(3)其他起源,如OutputStream socketOut = socket.getOutputStream();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值