readfile.java

来自「java的23中设计模式的代码实现」· Java 代码 · 共 44 行

JAVA
44
字号
/**
 *  A concrete class extends AbstractRead
 *  This class can read from a file
 */
import java.io.*;

public class ReadFile extends AbstractRead {
    private BufferedReader in = null;
    public ReadFile() {
    }
    public ReadFile(String fileName) {
        resource = fileName;
    }
    protected boolean open() {
        try {
            in = new BufferedReader(new FileReader(resource));
        } catch(IOException e) {
            System.out.println("Can not open file!");
            return false;
        }
        return true;
    }
    protected void readContent() {
        try {
            if(in != null) {
                String str;
                while((str = in.readLine()) != null) {
                     System.out.println(str);  
                }
            }
        } catch(IOException e) {
            System.out.println("Read file error !");
        }
    }
    protected void close() {
        if(in != null) {
            try {
                in.close();
            } catch(IOException e) {
                System.out.println("IO error !");
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?