FileOutputStream記錄


FileOutputStream提供了五個構造方法,分別是

public FileOutputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null, false);
}
FileOutputStream的構造方法允許直接傳入文件路徑,而無須使用File對象。查看該構造方法的源代碼,其內部使用了File對象打開文件。
如果filepath指定的文件不存在, 文件輸出流會幫我們自動創建一個文件。僅僅是文件而已。 如果filepath中包含尚未創建的目錄, 就會拋出文件找不到異常。 所以在使用輸出流之前, 最好使用File的mkdirs方法, 先創建文件的目錄信息
public FileOutputStream(String name, boolean append)
        throws FileNotFoundException{
        this(name != null ? new File(name) : null, append);
}
append參數為true時,數據從文件尾部寫入;append參數為false時,數據覆蓋原文件
public FileOutputStream(File file) throws FileNotFoundException {
        this(file, false);
}
使用File對象打開本地文件,向文件寫入數據
public FileOutputStream(File file, boolean append)
        throws FileNotFoundException
 {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        this.fd = new FileDescriptor();
        fd.attach(this);
        this.append = append;
        this.path = name;

        open(name, append);
}
append參數為true時,數據從文件尾部寫入;append參數為false時,數據覆蓋原文件
 public FileOutputStream(FileDescriptor fdObj) {
        SecurityManager security = System.getSecurityManager();
        if (fdObj == null) {
            throw new NullPointerException();
        }
        if (security != null) {
            security.checkWrite(fdObj);
        }
        this.fd = fdObj;
        this.append = false;
        this.path = null;

        fd.attach(this);
    }
該構造方法需要了解的話可以參考該博文:http://www.cnblogs.com/skywang12345/p/io_09.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM