標準 IO 鏈式構造冗長(如 new DataInputStream(new BufferedInputStream(new FileInputStream(...)))),封裝為單一類簡化使用。
public class InFile extends DataInputStream {
public InFile(String filename) throws FileNotFoundException {
super(new BufferedInputStream(new FileInputStream(filename)));
}
public InFile(File file) throws FileNotFoundException {
this(file.getPath());
}
}
public class PrintFile extends PrintStream {
public PrintFile(String filename) throws IOException {
super(new BufferedOutputStream(new FileOutputStream(filename)));
}
public PrintFile(File file) throws IOException {
this(file.getPath());
}
}
public class OutFile extends DataOutputStream {
public OutFile(String filename) throws IOException {
super(new BufferedOutputStream(new FileOutputStream(filename)));
}
public OutFile(File file) throws IOException {
this(file.getPath());
}
}
InFile in = new InFile("test.txt");
String line = in.readLine();
PrintFile out = new PrintFile("out.txt");
out.println("Hello");
OutFile data = new OutFile("data.bin");
data.writeDouble(3.14);