Java I/O系统之Reader

1.Reader类型

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


2.Reader的基本方法

1)        读取一个字符并以整数形式返回(0~255),如果返回-1已到输入流的末尾。

  1. int read() throws IOException  
int read() throws IOException

2)        读取一系列字符并存储到一个数组buffer,返回实际读取的字符数,如果读取前已到输入流的末尾返回-1

  1. int read(char[] cbuf) throws IOException  
int read(char[] cbuf) throws IOException

3)        读取length个字符,并存储到一个数组buffer,从length位置开始返回实际读取的字符数,如果读取前输入流的末尾返回-1.

  1. int read(char[] cbuf, int offset, int length) throws IOException  
int read(char[] cbuf, int offset, int length) throws IOException

4)        关闭流释放内存资源

  1. void close() throws IOException  
void close() throws IOException

5)        跳过n个字符不读,返回实际跳过的字节数

  1. long skip(long n) throws IOException  
long skip(long n) throws IOException

3.Reader例子

  1. package com.owen.io;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6.   
  7. /** 
  8.  * 读取文件 FileReader 
  9.  * @author OwenWilliam 2016-7-19 
  10.  * @since 
  11.  * @version v1.0.0 
  12.  * 
  13.  */  
  14. public class TestFileReader  
  15. {  
  16.   
  17.     public static void main(String[] args)  
  18.     {  
  19.         FileReader fr = null;  
  20.         int c = 0;  
  21.           
  22.         try  
  23.         {  
  24.             fr = new FileReader("E:/workspace/Java/IO/src/com/owen/io/TestFileReader.java");  
  25.             int ln = 0;  
  26.             while ((c = fr.read()) != -1)  
  27.             {  
  28.                 //char ch = (char) fr.read();  
  29.                 System.out.print((char) c);  
  30.                 /*if (++ln >= 100) 
  31.                 { 
  32.                     System.out.println(); 
  33.                     ln = 0; 
  34.                 }*/  
  35.             }  
  36.         } catch (FileNotFoundException e)  
  37.         {  
  38.             System.out.println("文件找不到");  
  39.         }catch (IOException e)  
  40.         {  
  41.             System.out.println("文件读取错误");  
  42.         }  
  43.     }  
  44.   
  45. }  
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("文件读取错误");
		}
	}

}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值