IO流——流的分类、InputStream、OutputStream、Reader、Writer等
一、IO流概述
流的分類
1.操作數(shù)據(jù)單位:字節(jié)流、字符流
2.數(shù)據(jù)的流向:輸入流、輸出流
3.流的角色:節(jié)點(diǎn)流、處理流
二、節(jié)點(diǎn)流 (文件流)
說(shuō)明點(diǎn):
將day09下的hello.txt文件內(nèi)容讀入程序中,并輸出到控制臺(tái)
- read()的理解:返回讀入的一個(gè)字符。如果達(dá)到文件末尾,返回-1
- 異常的處理:為了保證流資源一定可以執(zhí)行關(guān)閉操作。需要使用try-catch-finally處理
- 讀入的文件一定要存在,否則就會(huì)報(bào)FileNotFoundException。
從內(nèi)存中寫出數(shù)據(jù)到硬盤的文件里。
說(shuō)明:
- 輸出操作,對(duì)應(yīng)的File可以不存在的。并不會(huì)報(bào)異常
- File對(duì)應(yīng)的硬盤中的文件如果不存在,在輸出的過(guò)程中,會(huì)自動(dòng)創(chuàng)建此文件。
File對(duì)應(yīng)的硬盤中的文件如果存在:
如果流使用的構(gòu)造器是:FileWriter(file,false) / FileWriter(file):對(duì)原文件的覆蓋
如果流使用的構(gòu)造器是:FileWriter(file,true):不會(huì)對(duì)原文件覆蓋,而是在原文件基礎(chǔ)上追加內(nèi)容
三、緩沖流
1.緩沖流涉及到的類:* BufferedInputStream * BufferedOutputStream * BufferedReader * BufferedWriter2.作用: 作用:提供流的讀取、寫入的速度 提高讀寫速度的原因:內(nèi)部提供了一個(gè)緩沖區(qū)。默認(rèn)情況下是8kb3.典型代碼 3.1 使用BufferedInputStream和BufferedOutputStream:處理非文本文件 //實(shí)現(xiàn)文件復(fù)制的方法public void copyFileWithBuffered(String srcPath,String destPath){BufferedInputStream bis = null;BufferedOutputStream bos = null;try {//1.造文件File srcFile = new File(srcPath);File destFile = new File(destPath);//2.造流//2.1 造節(jié)點(diǎn)流FileInputStream fis = new FileInputStream((srcFile));FileOutputStream fos = new FileOutputStream(destFile);//2.2 造緩沖流bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);//3.復(fù)制的細(xì)節(jié):讀取、寫入byte[] buffer = new byte[1024];int len;while((len = bis.read(buffer)) != -1){bos.write(buffer,0,len);}} catch (IOException e) {e.printStackTrace();} finally {//4.資源關(guān)閉//要求:先關(guān)閉外層的流,再關(guān)閉內(nèi)層的流if(bos != null){try {bos.close();} catch (IOException e) {e.printStackTrace();}}if(bis != null){try {bis.close();} catch (IOException e) {e.printStackTrace();}}//說(shuō)明:關(guān)閉外層流的同時(shí),內(nèi)層流也會(huì)自動(dòng)的進(jìn)行關(guān)閉。關(guān)于內(nèi)層流的關(guān)閉,我們可以省略. // fos.close(); // fis.close();}}3.2 使用BufferedReader和BufferedWriter:處理文本文件 @Testpublic void testBufferedReaderBufferedWriter(){BufferedReader br = null;BufferedWriter bw = null;try {//創(chuàng)建文件和相應(yīng)的流br = new BufferedReader(new FileReader(new File("dbcp.txt")));bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));//讀寫操作//方式一:使用char[]數(shù)組 // char[] cbuf = new char[1024]; // int len; // while((len = br.read(cbuf)) != -1){ // bw.write(cbuf,0,len); // // bw.flush(); // }//方式二:使用StringString data;while((data = br.readLine()) != null){//方法一: // bw.write(data + "\n");//data中不包含換行符//方法二:bw.write(data);//data中不包含換行符bw.newLine();//提供換行的操作}} catch (IOException e) {e.printStackTrace();} finally {//關(guān)閉資源if(bw != null){try {bw.close();} catch (IOException e) {e.printStackTrace();}}if(br != null){try {br.close();} catch (IOException e) {e.printStackTrace();}}}}四、轉(zhuǎn)換流
-
InputStreamReader:將一個(gè)字節(jié)的輸入流轉(zhuǎn)換為字符的輸入流
解碼:字節(jié)、字節(jié)數(shù)組 —>字符數(shù)組、字符串 -
OutputStreamWriter:將一個(gè)字符的輸出流轉(zhuǎn)換為字節(jié)的輸出流
編碼:字符數(shù)組、字符串 —> 字節(jié)、字節(jié)數(shù)組
說(shuō)明:編碼決定了解碼的方式
作用:提供字節(jié)流與字符流之間的轉(zhuǎn)換
圖示:
典型實(shí)現(xiàn):
//文件編碼的方式(比如:GBK),決定了解析時(shí)使用的字符集(也只能是GBK)。
五、標(biāo)準(zhǔn)輸入輸出流、打印流、數(shù)據(jù)流 ( 了解 )
1. 標(biāo)準(zhǔn)的輸入輸出流: System.in:標(biāo)準(zhǔn)的輸入流,默認(rèn)從鍵盤輸入 System.out:標(biāo)準(zhǔn)的輸出流,默認(rèn)從控制臺(tái)輸出修改默認(rèn)的輸入和輸出行為: System類的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定輸入和輸出的流。2. 打印流: PrintStream 和PrintWriter 說(shuō)明: 提供了一系列重載的print()和println()方法,用于多種數(shù)據(jù)類型的輸出 System.out返回的是PrintStream的實(shí)例3. 數(shù)據(jù)流: DataInputStream 和 DataOutputStream 作用: 用于讀取或?qū)懗龌緮?shù)據(jù)類型的變量或字符串示例代碼: /* 練習(xí):將內(nèi)存中的字符串、基本數(shù)據(jù)類型的變量寫出到文件中。注意:處理異常的話,仍然應(yīng)該使用try-catch-finally.*/ @Test public void test3() throws IOException {//1.DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));//2.dos.writeUTF("劉建辰");dos.flush();//刷新操作,將內(nèi)存中的數(shù)據(jù)寫入文件dos.writeInt(23);dos.flush();dos.writeBoolean(true);dos.flush();//3.dos.close();} /* 將文件中存儲(chǔ)的基本數(shù)據(jù)類型變量和字符串讀取到內(nèi)存中,保存在變量中。注意點(diǎn):讀取不同類型的數(shù)據(jù)的順序要與當(dāng)初寫入文件時(shí),保存的數(shù)據(jù)的順序一致!*/ @Test public void test4() throws IOException {//1.DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));//2.String name = dis.readUTF();int age = dis.readInt();boolean isMale = dis.readBoolean();System.out.println("name = " + name);System.out.println("age = " + age);System.out.println("isMale = " + isMale);//3.dis.close();}六、對(duì)象流
ObjectInputStream和 ObjectOutputStream
ObjectOutputStream:內(nèi)存中的對(duì)象—>存儲(chǔ)中的文件、通過(guò)網(wǎng)絡(luò)傳輸出去:序列化過(guò)程
ObjectInputStream:存儲(chǔ)中的文件、通過(guò)網(wǎng)絡(luò)接收過(guò)來(lái) —>內(nèi)存中的對(duì)象:反序列化過(guò)程
對(duì)象序列化機(jī)制允許把內(nèi)存中的Java對(duì)象轉(zhuǎn)換成平臺(tái)無(wú)關(guān)的二進(jìn)制流,從而允許把這種二進(jìn)制流持久地保存在磁盤
上,或通過(guò)網(wǎng)絡(luò)將這種二進(jìn)制流傳輸?shù)搅硪粋€(gè)網(wǎng)絡(luò)節(jié)點(diǎn)。//當(dāng)其它程序獲取了這種二進(jìn)制流,就可以恢復(fù)成原來(lái)的Java對(duì)象
-
1.需要實(shí)現(xiàn)接口:Serializable
-
2.當(dāng)前類提供一個(gè)全局常量:serialVersionUID
-
3.除了當(dāng)前Person類需要實(shí)現(xiàn)Serializable接口之外,還必須保證其內(nèi)部所屬性
-
也必須是可序列化的。(默認(rèn)情況下,基本數(shù)據(jù)類型可序列化)
-
補(bǔ)充:ObjectOutputStream和ObjectInputStream不能序列化
-
static和transient修飾的成員變量
-
transient來(lái)修飾某個(gè)成員不被序列化!
總結(jié)
以上是生活随笔為你收集整理的IO流——流的分类、InputStream、OutputStream、Reader、Writer等的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 产品经理必须要掌握的12种思维模型
- 下一篇: JAVA JDBC详解