這是我的第一篇博客,紀念一下吧!
最近學習了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() 關閉此文件輸入流並釋放與流相關聯的任何系統資源。
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