java FileOutputStream輸出流的使用解讀
FileOutputStream的構(gòu)造方法
FileOutputStream提供了4個常用構(gòu)造方法,用于實例化FileOutputStream對象,
不同的場景使用不同的構(gòu)造方法。
場景1
使用File對象打開本地文件,從文件讀取數(shù)據(jù)。
public FileOutputStream(File file) throws FileNotFoundException{}查看底層源碼發(fā)現(xiàn)該構(gòu)造方法實際是調(diào)用了另一個構(gòu)造方法
public FileOutputStream(File file) throws FileNotFoundException {
? ? ? ? this(file, false);
? ? }場景2
不使用File對象,直接傳入文件路徑。
public FileOutputStream(String name) throws FileNotFoundException{}FileOutputStream的構(gòu)造方法允許直接傳入文件路徑,而無須使用File對象。查看該構(gòu)造方法的源代碼,其內(nèi)部使用了File對象打開文件。
場景3
打開文件,在文件的尾部追加寫入數(shù)據(jù)。
場景要求在文件的尾部寫入數(shù)據(jù),由于前面兩個構(gòu)造函數(shù)都是文件開始寫入數(shù)據(jù)(覆蓋原文件),因此不能使用前面2個場景的構(gòu)造函數(shù)。FileOutputStream提供了另外兩個構(gòu)構(gòu)造方法,分別是:
public FileOutputStream(File file,boolean append) throws FileNotFoundException{}
public FileOutputStream(String name,boolean append) throws FileNotFoundException{}同前面的構(gòu)造方法相比,這兩個構(gòu)造方法各多了一個boolean參數(shù)append。
append參數(shù)為true時,數(shù)據(jù)從文件尾部寫入;append參數(shù)為false時,數(shù)據(jù)覆蓋原文件。
這也是第一個方法調(diào)用的那個方法
FileOutputStream的寫入方法
FileOutputStream類提供了多種文件寫入方法,可以單獨寫一個字節(jié)到文件,
也可以寫一個byte數(shù)組到文件,也可以取byte數(shù)組的部分?jǐn)?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();
? ? ? ? }
? ? }
}例子程序首先調(diào)用File類的createNewFile()創(chuàng)建new.txt文件,然后將str內(nèi)容寫入到新創(chuàng)建的new.txt文件中。
例2
使用write(byte[] b)方法寫入文件。
write(byte[] b)方法用于將b.length個字節(jié)從指定的byte數(shù)組寫入到輸出流。
String類的getBytes()方法可以將字符串轉(zhuǎn)換為byte數(shù)組,使用FileOutputStream 類的write(byte[] b)方法,將轉(zhuǎn)換的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內(nèi)容寫入到文件,fos.write(str.getBytes(),5,10)語句的第一個參數(shù)為byte數(shù)組,第二個參數(shù)5是從byte數(shù)組的下標(biāo)5開始,第三個參數(shù)是寫入的字節(jié)數(shù)。程序執(zhí)行后,寫入的內(nèi)容為“is new file”。
使用該方法一定要注意數(shù)組越界的問題。例如,byte數(shù)組長度為20,從第下標(biāo)12開始,寫入15個字節(jié)到文件,就會造成數(shù)組越界,程序報錯。
例4
使用FileOutputStream復(fù)制文件
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();
? ? ? ? }
? ? }
}復(fù)制文件是將源文件數(shù)據(jù)寫入到新文件,在實際編程中,實現(xiàn)文件的復(fù)制有很多種方法,本案例使用FileInputStream和FileOutputStream實現(xiàn)文件的復(fù)制。
java中的IO流中的輸出流一般都有flush這個操作,這個操作的作用是強(qiáng)制將緩存中的輸出流(字節(jié)流,字符流等)強(qiáng)制輸出。
為什么會有這么個方法???
因為輸出流在進(jìn)行輸出時,比如像某個文件中寫入內(nèi)容,其實是先將輸出流寫入到緩沖區(qū),當(dāng)緩沖區(qū)寫滿后才將緩沖區(qū)的內(nèi)容輸出到文件中。但是當(dāng)主機(jī)完成輸出流的輸出后,有可能緩沖區(qū)這個時候還沒有被填滿,這
樣的話,就會一直等待主機(jī)發(fā)送內(nèi)容,這時候,就可以使用flush將緩沖區(qū)的內(nèi)容強(qiáng)制輸出到文件中,清空緩沖區(qū)。
所以,一般在關(guān)閉輸出流之前,要先調(diào)用flush方法強(qiáng)制緩沖區(qū)中的內(nèi)容輸出,并清空緩沖區(qū)。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決IDEA使用springBoot創(chuàng)建項目,lombok標(biāo)注實體類后編譯無報錯,但是運行時報錯問題
文章詳細(xì)描述了在使用lombok的@Data注解標(biāo)注實體類時遇到編譯無誤但運行時報錯的問題,分析了可能的原因,并提供了解決步驟2025-01-01
SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list
這篇文章主要介紹了SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringCloud Finchley Gateway 緩存請求Body和Form表單的實現(xiàn)
在接入Spring-Cloud-Gateway時,可能有需求進(jìn)行緩存Json-Body數(shù)據(jù)或者Form-Urlencoded數(shù)據(jù)的情況。這篇文章主要介紹了SpringCloud Finchley Gateway 緩存請求Body和Form表單的實現(xiàn),感興趣的小伙伴們可以參考一下2019-01-01
純Java實現(xiàn)數(shù)字證書生成簽名的簡單實例
下面小編就為大家?guī)硪黄僇ava實現(xiàn)數(shù)字證書生成簽名的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08

