刚才在看帮助文档的时候,发现Java io包中FilterReader和FilterWriter类没使用过,所以想实践一下。
还用以前的老方法,new一个出来,结果提示有错误:
Cannot instantiate the type FilterReader
不能实例化?
一查帮助文档原来这样写道:
public abstract class FilterReader
extends Reader
Abstract class for reading filtered character streams
原来是抽象类啊,因为之前在面象对象这块,没怎么用抽象类,都是纸上谈兵(欠下的东西迟早要还的)。
吓得我赶紧翻大神的博客来看:深入理解Java的接口和抽象类,最后搞定!
代码如下:
1、先定义一个类FD,该类继承抽象类FilterReader
1 public class FD extends FilterReader { 2 public FD(BufferedReader br) { 3 super(br); 4 // TODO Auto-generated constructor stub 5 } 6 }
2、实例化FD
1 public void test() throws IOException { 2 BufferedReader br = null; 3 br = new BufferedReader(new InputStreamReader(new FileInputStream("E:/syl/1.html"), "UTF-8")); 4 FD fd = new FD(br); 5 int i = 0; 6 char[] b = new char[1024]; 7 int len = 0; 8 while ((len = fd.read(b)) != -1) { 9 System.out.println(b); 10 } 11 fd.close(); 12 }
其中上面的第3行代码等于:
1 FileInputStream in = new FileInputStream("E:/syl/1.html"); 2 InputStreamReader inReader = new InputStreamReader(in, "UTF-8"); 3 BufferedReader br = new BufferedReader(inReader);
时间: 2024-10-05 23:09:13