首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 快捷文件處理類:InFile、PrintFile、OutFile

     編輯:Java 相關知識

動機

標準 IO 鏈式構造冗長(如 new DataInputStream(new BufferedInputStream(new FileInputStream(...)))),封裝為單一類簡化使用。

InFile - 輸入快捷類

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());
    }
}

PrintFile - 格式化輸出快捷類

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());
    }
}

OutFile - 數據輸出快捷類

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);
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved