Java I/O系统之Writer

来源:互联网 发布:c罗会说几种语言 编辑:程序博客网 时间:2024/06/13 07:55

1.Writer类型

继承Writer的流都是用于程序输出数据,且数据的单位为字符(16bit);下图中深色为节点流,浅色为处理流。


2.Writer的基本方法

1)        向输出流写入一个字符数据,该字节数据为参数b的低16位

[java] view plain copy
print?
  1. void write(int c) throws IOException  

2)        将一个字符类型的数组中的数据写入输出流。

[java] view plain copy
print?
  1. void write(char[] cbuf) throws IOException  

3)        将一个字符类型的数组中的从指定位置(offset)开始length个字符写入到输出流。

[java] view plain copy
print?
  1. void write(char[] cbuf, int offset, int length) throws IOException  

4)        将一个字符串中的字符写入到输出流

[java] view plain copy
print?
  1. void write(String string) thows IOException  

5)        将一个字符串从offset开始的length个字节写入到输出流

[java] view plain copy
print?
  1. void write(String string, int offset, int length) thows IOException  

6)        关闭流释放内存资源

[java] view plain copy
print?
  1. void close() throws IOException  

7)        将输出流中缓存的数据全部写出到目的地

[java] view plain copy
print?
  1. void flush() throws IOException  

3.Writer例子

[java] view plain copy
print?
  1. package com.owen.io;  
  2.   
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5.   
  6. /** 
  7.  * 写入文件 FileWriter 
  8.  * @author OwenWilliam 2016-7-19 
  9.  * @since 
  10.  * @version v1.0.0 
  11.  * 
  12.  */  
  13. public class TestFileWriter  
  14. {  
  15.   
  16.     public static void main(String[] args)  
  17.     {  
  18.   
  19.         FileWriter fw = null;  
  20.           
  21.         try  
  22.         {  
  23.             fw = new FileWriter("E:/workspace/Java/IO/src/com/owen/io/unit.dat");  
  24.             for (int c = 0; c <= 50000; c++)  
  25.             {  
  26.                 fw.write(c);  
  27.             }  
  28.             fw.close();  
  29.         } catch (IOException e)  
  30.         {  
  31.             e.printStackTrace();  
  32.             System.out.println("文件写入错误");  
  33.             System.exit(-1);  
  34.         }  
  35.     }  
  36. }