java FileOutputStream輸出流的使用解讀
FileOutputStream的構造方法
FileOutputStream提供了4個常用構造方法,用于實例化FileOutputStream對象,
不同的場景使用不同的構造方法。
場景1
使用File對象打開本地文件,從文件讀取數(shù)據(jù)。
public FileOutputStream(File file) throws FileNotFoundException{}
查看底層源碼發(fā)現(xiàn)該構造方法實際是調用了另一個構造方法
public FileOutputStream(File file) throws FileNotFoundException { ? ? ? ? this(file, false); ? ? }
場景2
不使用File對象,直接傳入文件路徑。
public FileOutputStream(String name) throws FileNotFoundException{}
FileOutputStream的構造方法允許直接傳入文件路徑,而無須使用File對象。查看該構造方法的源代碼,其內部使用了File對象打開文件。
場景3
打開文件,在文件的尾部追加寫入數(shù)據(jù)。
場景要求在文件的尾部寫入數(shù)據(jù),由于前面兩個構造函數(shù)都是文件開始寫入數(shù)據(jù)(覆蓋原文件),因此不能使用前面2個場景的構造函數(shù)。FileOutputStream提供了另外兩個構構造方法,分別是:
public FileOutputStream(File file,boolean append) throws FileNotFoundException{} public FileOutputStream(String name,boolean append) throws FileNotFoundException{}
同前面的構造方法相比,這兩個構造方法各多了一個boolean參數(shù)append。
append參數(shù)為true時,數(shù)據(jù)從文件尾部寫入;append參數(shù)為false時,數(shù)據(jù)覆蓋原文件。
這也是第一個方法調用的那個方法
FileOutputStream的寫入方法
FileOutputStream類提供了多種文件寫入方法,可以單獨寫一個字節(jié)到文件,
也可以寫一個byte數(shù)組到文件,也可以取byte數(shù)組的部分數(shù)據(jù)寫入到文件。
例1
使用write(int b)方法寫入文件。
package com.demo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo { ? ? public static void main(String[] args) throws IOException { ? ? ? ? File file = new File("d://new.txt"); ? ? ? ? try { ? ? ? ? ? ? file.createNewFile(); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? try { ? ? ? ? ? ? FileOutputStream fileOutputStream = new FileOutputStream(file); ? ? ? ? ? ? String str = "this is new file"; ? ? ? ? ? ? for (int i = 0; i < str.length(); i++) { ? ? ? ? ? ? ? ? int b = (int)str.charAt(i); ? ? ? ? ? ? ? ? fileOutputStream.write(b); ? ? ? ? ? ? } ? ? ? ? } catch (FileNotFoundException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
例子程序首先調用File類的createNewFile()創(chuàng)建new.txt文件,然后將str內容寫入到新創(chuàng)建的new.txt文件中。
例2
使用write(byte[] b)方法寫入文件。
write(byte[] b)方法用于將b.length個字節(jié)從指定的byte數(shù)組寫入到輸出流。
String類的getBytes()方法可以將字符串轉換為byte數(shù)組,使用FileOutputStream 類的write(byte[] b)方法,將轉換的byte數(shù)組寫入文件。
package com.demo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo { ? ? public static void main(String[] args) throws IOException { ? ? ? ? File file = new File("d://new.txt"); ? ? ? ? try { ? ? ? ? ? ? file.createNewFile(); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? try { ? ? ? ? ? ? FileOutputStream fileOutputStream = new FileOutputStream(file); ? ? ? ? ? ? String str = "this is new file"; ? ? ? ? ? ? fileOutputStream.write(str.getBytes()); ? ? ? ? } catch (FileNotFoundException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
例3
使用write(byte[] b,int off,int len)方法寫入文件。
該方法將len個字節(jié)的數(shù)據(jù),并從數(shù)組b的off位置開始寫入到輸出流。
package com.demo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo { ? ? public static void main(String[] args) throws IOException { ? ? ? ? File file = new File("d://new.txt"); ? ? ? ? try { ? ? ? ? ? ? file.createNewFile(); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? try { ? ? ? ? ? ? FileOutputStream fileOutputStream = new FileOutputStream(file); ? ? ? ? ? ? String str = "this is new file"; ? ? ? ? ? ? fileOutputStream.write(str.getBytes(),5,11); ? ? ? ? } catch (FileNotFoundException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
程序把指定的str內容寫入到文件,fos.write(str.getBytes(),5,10)語句的第一個參數(shù)為byte數(shù)組,第二個參數(shù)5是從byte數(shù)組的下標5開始,第三個參數(shù)是寫入的字節(jié)數(shù)。程序執(zhí)行后,寫入的內容為“is new file”。
使用該方法一定要注意數(shù)組越界的問題。例如,byte數(shù)組長度為20,從第下標12開始,寫入15個字節(jié)到文件,就會造成數(shù)組越界,程序報錯。
例4
使用FileOutputStream復制文件
package com.demo; import java.io.*; public class Demo { ? ? public static void main(String[] args) { ? ? ? ? File source = new File("d://new.txt"); ? ? ? ? File dest = new File("d://new2.txt"); ? ? ? ? copy(source,dest); ? ? } ? ? public static void copy(File sor, File dest){ ? ? ? ? if(!sor.exists()){ ? ? ? ? ? ? System.out.println("源文件不存在"); ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? try { ? ? ? ? ? ? FileInputStream in = new FileInputStream(sor); ? ? ? ? ? ? FileOutputStream out = new FileOutputStream(dest); ? ? ? ? ? ? byte[] buf = new byte[in.available()]; ? ? ? ? ? ? in.read(buf); ? ? ? ? ? ? out.write(buf); ? ? ? ? ? ? out.flush(); ? ? ? ? ? ? out.close(); ? ? ? ? ? ? in.close(); ? ? ? ? } catch (FileNotFoundException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
復制文件是將源文件數(shù)據(jù)寫入到新文件,在實際編程中,實現(xiàn)文件的復制有很多種方法,本案例使用FileInputStream和FileOutputStream實現(xiàn)文件的復制。
java中的IO流中的輸出流一般都有flush這個操作,這個操作的作用是強制將緩存中的輸出流(字節(jié)流,字符流等)強制輸出。
為什么會有這么個方法?。?/strong>
因為輸出流在進行輸出時,比如像某個文件中寫入內容,其實是先將輸出流寫入到緩沖區(qū),當緩沖區(qū)寫滿后才將緩沖區(qū)的內容輸出到文件中。但是當主機完成輸出流的輸出后,有可能緩沖區(qū)這個時候還沒有被填滿,這
樣的話,就會一直等待主機發(fā)送內容,這時候,就可以使用flush將緩沖區(qū)的內容強制輸出到文件中,清空緩沖區(qū)。
所以,一般在關閉輸出流之前,要先調用flush方法強制緩沖區(qū)中的內容輸出,并清空緩沖區(qū)。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java面試題沖刺第十二天--數(shù)據(jù)庫(2)
這篇文章主要為大家分享了最有價值的三道數(shù)據(jù)庫面試題,涵蓋內容全面,包括數(shù)據(jù)結構和算法相關的題目、經典面試編程題等,感興趣的小伙伴們可以參考一下2021-07-07