精品计算机课件Java程序基础教程第讲Java中的字符流.pdf
文本预览下载声明
Java 语言程序设计
第十三讲 Java 中的字符流
主要内容
Reader 和Writer
FileReader 和FileWriter
其他字符I/O 流
Reader 和Writer
Reader
抽象类,规定了字符输入流的最基本操作——读操作。
read 方法
public int read() throws IOException
从流中读一个字符,当流中没有数据可读时,返回值为-1
public int read(char[] c) throws IOException
根据数组尺寸从流中读字符,置于数组中。返回读取字符个数。
public int read(char[] c,int off,int len) throws
IOException
读入len 个字符,从下标off 开始置入数组cbuf
其他方法
skip 方法:从流中跳过字符。
public long skip(long n) throws IOException
close 方法:关闭流。
public abstract void close() throws IOException
思考
1. Reader 子类对象,表示的是怎样的一种数据流?
2. 对于Reader 数据流,如何从中读取数据?
3. 对于Reader 数据流,如何一次性读取一组数据?
4. 在数据流操作时,将会抛出什么异常?
5. 如何创建一个Reader 子类对象?
Java 语言程序设计
Writer
抽象类,规定了字符输出流的最基本操作——写操作。
写方法
public void write (int b) throws IOException
向流中写一个字符数据
public void write (char[] cbuf) throws IOException
向流中写字符数组cbuf
public void write(String s) throws IOException
向流中写一个字符串(很实用)
...
flush 方法
public abstract void flush() throws IOException
将缓冲区中数据立即写入到输出流。
close 方法
public abstract void close() throws IOException
思考
1. Writer 子类对象,表示的是怎样的一种数据流?
2. 对于Writer 数据流,如何向它写入数据?可以写入什么样
的数据?
3. 如何关闭Reader 数据流和Writer 数据流?
4. 关闭数据流意味着什么?
5. 如何创建一个Writer 子类对象?
FileReader 和FileWriter
按字符对文件进行数据读入操作。更适合于对文本文件的操作。
F
显示全部