FileInputStream與FileOutputStream學習筆記
這是我的第一篇部落格,紀念一下吧!
最近學習了IO流,想著學長說的話,就突然想要寫寫部落格了,別管這是什麼邏輯了,進入正題。
一.FileInputStream
1.概念
FileInputStream是Java語言中抽象類InputStream用來具體實現類的建立物件。FileInputStream可以從檔案系統中的某個檔案中獲得輸入位元組,獲取的檔案可用性取決於主機環境。--百度百科
FileInputStream是節點流,是位元組流
2.API文件
1)構造方法:
FileInputStream(File file)
通過開啟與實際檔案的連線來建立一個 FileInputStream
,該檔案由檔案系統中的 File
物件 file
命名
FileInputStream(String name)
通過開啟與實際檔案的連線來建立一個 FileInputStream
,該檔案由檔案系統中的路徑名 name
命名。
2)常用方法:
int read() 從該輸入流讀取一個位元組資料
int read() 從該輸入流讀取最多b.length個位元組的資料。
void close() 關閉此檔案輸入流並釋放與流相關聯的任何系統資源。
3.程式碼1)從輸入流中一個位元組一個位元組讀取
public class IOTest2 { public static void main(String[] args) { //建立File類物件 File src = new File("abc.txt");//多型 InputStream is = null; try { is = new FileInputStream(src); //操作 int temp; /* * 返回的是讀取到的一個位元組資料對應的int值 * 返回值為-1時說明檔案讀取完畢 * */ while((temp=is.read()) != -1) { //這裡強制轉換為char型別 System.out.print((char)temp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { //關閉流 is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2)將流中資料讀取到位元組陣列中
public class IOTest3 { public static void main(String[] args) { //建立源 File src = new File("abc.txt"); //選擇流 InputStream is = null; try { is = new FileInputStream(src); byte[] buffer = new byte[5]; int len = -1; /*從當前位置從輸入流中讀取b.length個位元組的資料存放到flush中, 實際讀取到的資料要看輸入流中的位元組數, 最多讀取的數量就是flush.length,返回值為實際讀取的位元組數 */ while((len = is.read(buffer)) != -1) { //將輸入流中的資料讀取到flush容器中,從flush容器偏移量為0的地方開始,讀取最多這麼多個位元組 //用得到的flush陣列構造字串,從flush陣列偏移量為0處開始 String str = new String(buffer, 0, len); //len是當前flush的實際長度 System.out.println(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
注:這裡解釋一下偏移量的概念。就是假如我位元組陣列下標為0~10,現在偏移量為1,那麼位元組資料就是從位元組陣列下標為1的地方開始存放,同時要注意既然偏移了1個單位,那麼位元組陣列可存放的資料個數就為10(1~10)個了
二、FileOutputStream
1.概念
檔案輸出流是用於將資料寫入 File 或 FileDescriptor 的輸出流 -百度百科
2.API文件
1)構造方法:
FileOutputStream(File file)
建立檔案輸出流以寫入由指定的
File
物件表示的檔案。
FileOutputStream(File
file, boolean append)
建立檔案輸出流以寫入由指定的
File
物件表示的檔案。(可追加寫出)
FileOutputStream(String name) 建立檔案輸出流以指定的名稱寫入檔案。
FileOutputStream(String name, boolean append) 建立檔案輸出流以指定名稱寫入檔案
2)常用方法
void
write(int b)
將指定位元組寫入到此檔案輸出流。
void
write(byte[] b)
將 b.length
位元組從指定的位元組陣列寫入此檔案輸出流。
void
write(byte[] b, int off, int len)
將 len
位元組從指定的位元組陣列開始,從偏移量 off
開始寫入此檔案輸出流。
3.程式碼
public class IOTest4 { public static void main(String[] args) { File src = new File("nb.txt"); //檔案不存在會自動建立 OutputStream os = null; try { //os = new FileOutputStream(src); os = new FileOutputStream(src,true); //append標識,追加寫入 //操作 String message = "change\r\n"; //寫入的資訊 byte[] data = message.getBytes(); //轉換成位元組陣列(編碼) //將位元組陣列中的資料寫入到輸出流中 os.write(data, 0, data.length); os.flush(); //寫入後及時重新整理流 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
最後再附上一張圖和一篇很好的部落格
https://blog.csdn.net/jeryjeryjery/article/details/72236643