Java I/O系统之Writer

来源:互联网 发布:小程序 数据显示 编辑:程序博客网 时间:2024/06/08 08:44

1.Writer类型

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


2.Writer的基本方法

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

void write(int c) throws IOException

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

void write(char[] cbuf) throws IOException

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

void write(char[] cbuf, int offset, int length) throws IOException

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

void write(String string) thows IOException

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

void write(String string, int offset, int length) thows IOException

6)        关闭流释放内存资源

void close() throws IOException

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

void flush() throws IOException

3.Writer例子

package com.owen.io;import java.io.FileWriter;import java.io.IOException;/** * 写入文件 FileWriter * @author OwenWilliam 2016-7-19 * @since * @version v1.0.0 * */public class TestFileWriter{public static void main(String[] args){FileWriter fw = null;try{fw = new FileWriter("E:/workspace/Java/IO/src/com/owen/io/unit.dat");for (int c = 0; c <= 50000; c++){fw.write(c);}fw.close();} catch (IOException e){e.printStackTrace();System.out.println("文件写入错误");System.exit(-1);}}}









0 0
原创粉丝点击