1.Reader类型
继承自Reader的流都是用于向程序中输入数据,且数据的单位为字符(16bit);下图中深色为节点流,浅色的为处理流。
2.Reader的基本方法
1) 读取一个字符并以整数形式返回(0~255),如果返回-1已到输入流的末尾。
- int read() throws IOException
int read() throws IOException
2) 读取一系列字符并存储到一个数组buffer,返回实际读取的字符数,如果读取前已到输入流的末尾返回-1
- int read(char[] cbuf) throws IOException
int read(char[] cbuf) throws IOException
3) 读取length个字符,并存储到一个数组buffer,从length位置开始返回实际读取的字符数,如果读取前输入流的末尾返回-1.
- int read(char[] cbuf, int offset, int length) throws IOException
int read(char[] cbuf, int offset, int length) throws IOException
4) 关闭流释放内存资源
- void close() throws IOException
void close() throws IOException
5) 跳过n个字符不读,返回实际跳过的字节数
- long skip(long n) throws IOException
long skip(long n) throws IOException
3.Reader例子
- package com.owen.io;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- /**
- * 读取文件 FileReader
- * @author OwenWilliam 2016-7-19
- * @since
- * @version v1.0.0
- *
- */
- public class TestFileReader
- {
- public static void main(String[] args)
- {
- FileReader fr = null;
- int c = 0;
- try
- {
- fr = new FileReader("E:/workspace/Java/IO/src/com/owen/io/TestFileReader.java");
- int ln = 0;
- while ((c = fr.read()) != -1)
- {
- //char ch = (char) fr.read();
- System.out.print((char) c);
- /*if (++ln >= 100)
- {
- System.out.println();
- ln = 0;
- }*/
- }
- } catch (FileNotFoundException e)
- {
- System.out.println("文件找不到");
- }catch (IOException e)
- {
- System.out.println("文件读取错误");
- }
- }
- }
package com.owen.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* 读取文件 FileReader
* @author OwenWilliam 2016-7-19
* @since
* @version v1.0.0
*
*/
public class TestFileReader
{
public static void main(String[] args)
{
FileReader fr = null;
int c = 0;
try
{
fr = new FileReader("E:/workspace/Java/IO/src/com/owen/io/TestFileReader.java");
int ln = 0;
while ((c = fr.read()) != -1)
{
//char ch = (char) fr.read();
System.out.print((char) c);
/*if (++ln >= 100)
{
System.out.println();
ln = 0;
}*/
}
} catch (FileNotFoundException e)
{
System.out.println("文件找不到");
}catch (IOException e)
{
System.out.println("文件读取错误");
}
}
}