什么是IO流
存儲和讀取數據的解決方案
I:input
O:output
流:像水流一樣傳輸數據
IO流的作用
用于讀寫數據(本地文件,網絡)
IO流的分類
按照流向分類
輸出流:程序 --> 文件
輸入流:文件 --> 程序
按照操作文件的類型分類
字節流:可以操作所有類型的文件
字符流:只能操作純文本文件(用windows系統自帶的記事本打開并且能讀懂的文件,例如txt文件,md文件,xml文件,lrc文件等)
字節流的實現類
操作本地文件的字節輸出流FileOutputStream
可以把程序中的數據寫到本地文件中
?書寫步驟及細節
1.創建字節輸出流對象
????????參數是字符串表示的路徑或者File對象都是可以的
????????如果文件不存在會創建一個新的文件,但是要保證父級路徑是存在的。
????????如果文件已經存在,則會清空文件重新寫
2.寫數據? ? ? ? 使用write方法,參數為整數,但實際上寫到本地文件中的是整數在ASCII上對應的字符
3.釋放資源?????????每次使用完流之后都要釋放資源?
代碼演示
import java.io.FileOutputStream;
import java.io.IOException;public class Test1 {public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("day04\\a.txt");fos.write(97);fos.close();}
}
如何一次寫入多個數據
方法名稱 | 說明 |
void write(int b) | 一次寫一個字節數據 |
void write(byte[] b) | 一次寫一個字節數組數據 |
void write(byte[] b, int off, int len) | 一次寫一個字節數組的部分數據 |
代碼演示
import java.io.FileOutputStream;
import java.io.IOException;public class Test2 {public static void main(String[] args) throws IOException {//一次寫一個字節數組數據//有兩種定義數據的形式//直接定義byte數組,將想寫入的數據的ASCII碼存入數組FileOutputStream fos1 = new FileOutputStream("day04\\a.txt");byte[] b1 = {97,98,99,100};fos1.write(b1);fos1.close();//以字符串的形式定義想寫入的數據,再將字符串轉換為byte數組FileOutputStream fos2 = new FileOutputStream("day04\\a.txt");String str = "asdfg";byte[] b2 = str.getBytes();fos2.write(b2);fos2.close();//一次寫一個字節數組的部分數據//第二個參數是字節數組的起始索引//第三個參數是要寫入數據的個數FileOutputStream fos3 = new FileOutputStream("day04\\a.txt");byte[] b3 = {97,98,99,100,101,102};fos3.write(b3,1,3);fos3.close();}
}
如何換行寫
換行寫:使用換行符
windows:\r\n
Linux:\n
Mac:\r
細節:
在windows操作系統當中,java對回車換行進行了優化。
雖然完整的是\r\n,但是我們寫其中一個\r或者\n,也可以實現換行,java在底層會補全
建議:不要省略,還是寫全了
代碼演示
import java.io.FileOutputStream;
import java.io.IOException;public class Test3 {public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("day04\\a.txt");String str1 = "abcd";byte[] b1 = str1.getBytes();fos.write(b1);String wrap = "\r\n";byte[] b2 = wrap.getBytes();fos.write(b2);String str3 = "efgh";byte[] b3 = str3.getBytes();fos.write(b3);fos.close();}
}
?
如何續寫
如果想要續寫,打開續寫開關即可
開關位置:創建字節輸出流對象的第二個參數
不寫默認false:表示關閉續寫,此時創建字節輸出流對象會清空文件
手動傳遞true:表示打開續寫,此時創建字節輸出流對象不會清空文件
代碼演示
import java.io.FileOutputStream;
import java.io.IOException;public class Test4 {public static void main(String[] args) throws IOException {FileOutputStream fos1 = new FileOutputStream("day04\\a.txt");String str1 = "abcd";byte[] b1 = str1.getBytes();fos1.write(b1);fos1.close();FileOutputStream fos2 = new FileOutputStream("day04\\a.txt",true);String str2 = "efgh";byte[] b2 = str2.getBytes();fos2.write(b2);fos2.close();}
}
?
操作本地文件的字節輸入流FilelnputStream
可以把本地文件中的數據讀取到程序中來
書寫步驟及細節
1.創建字節輸入流對象
????????如果文件不存在,就直接報錯
2.讀數據? ? ? ? 利用read方法讀數據,讀出來的是數據在ASCII上對應的數字
????????讀到文件末尾讀不到數據時,read方法返回-1
3.釋放資源????????每次使用完流必須要釋放資源。
代碼演示
import java.io.FileInputStream;
import java.io.IOException;public class Test5 {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("day04\\a.txt");int b;//空參read一次只能讀取一個數據,利用循環進行讀取//直到讀到末尾讀不到數據時返回-1,結束循環while ((b = fis.read()) != -1) {//讀到的數據是ASCII字符對應的整數//通過char轉化為字符類型System.out.print((char) b);}//abcdefgfis.close();}
}
如何一次讀取多個數據
方法名稱 | 說明 |
public int read() | 一次讀一個字節數據 |
public int read(byte[] buffer) | 一次讀一個字節數組數據 每次讀取會盡可能把數組裝滿 返回讀取到的數據個數,讀不到返回-1 讀取的數據字符在byte數組中 |
代碼演示
import java.io.FileInputStream;
import java.io.IOException;public class Test6 {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("day04\\a.txt");//定義字節數組接收數據byte[] b = new byte[3];int len;//返回讀取到的數據個數,讀不到返回-1,用len記錄while ((len = fis.read(b)) != -1) {//讀取的數據會存放在byte數組中//新一輪讀取的數據會覆蓋在byte數組中的舊數據//最后一次讀取時,會出現數據讀不滿byte數組的情況//此時,byte數組中后面的部分數據不會被覆蓋//所以需要len(讀取到的數據個數),讀取幾個輸出幾個String str = new String(b, 0, len);System.out.println(str);}//abc//def//gfis.close();}
}