您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java的IO流知識點有哪些”,在日常操作中,相信很多人在Java的IO流知識點有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java的IO流知識點有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
字節(jié)流和字符流:
字節(jié)流:以字節(jié)為單位,每次次讀入或讀出是8位數(shù)據(jù)??梢宰x任何類型數(shù)據(jù)。
字符流:以字符為單位,每次次讀入或讀出是16位數(shù)據(jù)。其只能讀取字符類型數(shù)據(jù)。
輸出流和輸入流:
輸出流:從內(nèi)存讀出到文件。只能進行寫操作。
輸入流:從文件讀入到內(nèi)存。只能進行讀操作。
注意:這里的出和入,都是相對于系統(tǒng)內(nèi)存而言的。
節(jié)點流和處理流:
節(jié)點流:直接與數(shù)據(jù)源相連,讀入或讀出。
處理流:與節(jié)點流一塊使用,在節(jié)點流的基礎(chǔ)上,再套接一層,套接在節(jié)點流上的就是處理流。
為什么要有處理流?直接使用節(jié)點流,讀寫不方便,為了更快的讀寫文件,才有了處理流。
按操作方式分類結(jié)構(gòu)圖:
根據(jù)以上分類,以及jdk的說明,我們可以畫出更詳細的類結(jié)構(gòu)圖,如下:
分類說明
1. 輸入字節(jié)流InputStream:
輸入字節(jié)流的繼承圖可見上圖,可以看出:
FileInputStream: 是三種基本的介質(zhì)流,它們分別從Byte 數(shù)組、StringBuffer、和本地文件中讀取數(shù)據(jù)。
ByteArrayInputStream:
PipedInputStream: 是從與其它線程共用的管道中讀取數(shù)據(jù)。PipedInputStream的一個實例要和PipedOutputStream的一個實例共同使用,共同完成管道的讀取寫入操作。主要用于線程操作。
ObjectInputStream 和所有FilterInputStream 的子類都是裝飾流(裝飾器模式的主角)
2. 輸出字節(jié)流OutputStream:
輸出字節(jié)流的繼承圖可見上圖,可以看出:
FIleOutputStream:是兩種基本的介質(zhì)流
ByteArrayOutputStream: 是兩種基本的介質(zhì)流,它們分別向Byte 數(shù)組、和本地文件中寫入數(shù)據(jù)。
PipedOutputStream:是向與其它線程共用的管道中寫入數(shù)據(jù)。
ObjectOutputStream 和所有FilterOutputStream 的子類都是裝飾流。
字節(jié)流的輸入和輸出對照圖:
3. 字符輸入流Reader:
在上面的繼承關(guān)系圖中可以看出:
FileReader:
PipedReader:是從與其它線程共用的管道中讀取數(shù)據(jù)
CharArrayReader:
CharReader、StringReader 是兩種基本的介質(zhì)流,它們分別將Char 數(shù)組、String中讀取數(shù)據(jù)。
BufferedReader 很明顯就是一個裝飾器,它和其子類負責裝飾其它Reader 對象。
FilterReader 是所有自定義具體裝飾流的父類,其子類PushbackReader 對Reader 對象進行裝飾,會增加一個行號。
InputStreamReader: 是一個連接字節(jié)流和字符流的橋梁,它將字節(jié)流轉(zhuǎn)變?yōu)樽址?。FileReader 可以說是一個達到此功能、常用的工具類,在其源代碼中明顯使用了將FileInputStream 轉(zhuǎn)變?yōu)镽eader 的方法。我們可以從這個類中得到一定的技巧。Reader 中各個類的用途和使用方法基本和InputStream 中的類使用一致。后面會有Reader 與InputStream 的對應(yīng)關(guān)系。
4. 字符輸出流Writer:
在上面的關(guān)系圖中可以看出:
FileWriter:
PipedWriter:是向與其它線程共用的管道中寫入數(shù)據(jù)
CharArrayWriter:
CharArrayWriter、StringWriter 是兩種基本的介質(zhì)流,它們分別向Char 數(shù)組、String 中寫入數(shù)據(jù)。
BufferedWriter 是一個裝飾器,為Writer 提供緩沖功能。
PrintWriter 和PrintStream 極其類似,功能和使用也非常相似。
OutputStreamWriter: 是OutputStream 到Writer 轉(zhuǎn)換的橋梁,它的子類FileWriter 其實就是一個實現(xiàn)此功能的具體類(具體可以研究一SourceCode)。功能和使用和OutputStream 極其類似,后面會有它們的對應(yīng)圖。
字符流的輸入和輸出對照圖:
5. 字符流與字節(jié)流轉(zhuǎn)換
轉(zhuǎn)換流的特點:
其是字符流和字節(jié)流之間的橋梁;
可對讀取到的字節(jié)數(shù)據(jù)經(jīng)過指定編碼轉(zhuǎn)換成字符;
可對讀取到的字符數(shù)據(jù)經(jīng)過指定編碼轉(zhuǎn)換成字節(jié);
何時使用轉(zhuǎn)換流?
當字節(jié)和字符之間有轉(zhuǎn)換動作時;
流操作的數(shù)據(jù)需要編碼或解碼時。
具體的實現(xiàn):
InputStreamReader:輸入流轉(zhuǎn)到讀流;
String fileName= "d:"+File.separator+"hello.txt"; File file=new File(fileName); Writer out=new OutputStreamWriter(new FileOutputStream(file)); out.write("hello"); out.close();
OutputStreamWriter:輸出流轉(zhuǎn)到寫流;
String fileName= "d:"+File.separator+"hello.txt"; File file=new File(fileName); Reader read=new InputStreamReader(new FileInputStream(file)); char[] b=new char[100]; int len=read.read(b); System.out.println(new String(b,0,len)); read.close();
這兩個流對象是字符體系中的成員,它們有轉(zhuǎn)換作用,本身又是字符流,所以在構(gòu)造的時候需要傳入字節(jié)流對象進來。
按操作對象分類結(jié)構(gòu)圖:
分類說明:
對文件進行操作(節(jié)點流):
FileInputStream(字節(jié)輸入流),
FileOutputStream(字節(jié)輸出流),
FileReader(字符輸入流),
FileWriter(字符輸出流)
對管道進行操作(節(jié)點流):
PipedInputStream(字節(jié)輸入流),
PipedOutStream(字節(jié)輸出流),
PipedReader(字符輸入流),
PipedWriter(字符輸出流)。
PipedInputStream的一個實例要和PipedOutputStream的一個實例共同使用,共同完成管道的讀取寫入操作。主要用于線程操作。
字節(jié)/字符數(shù)組流(節(jié)點流):
ByteArrayInputStream,
ByteArrayOutputStream,
CharArrayReader,
CharArrayWriter;
是在內(nèi)存中開辟了一個字節(jié)或字符數(shù)組。
除了上述三種是節(jié)點流,其他都是處理流,需要跟節(jié)點流配合使用。
Buffered緩沖流(處理流):
BufferedInputStream,
BufferedOutputStream,
BufferedReader,
BufferedWriter,
是帶緩沖區(qū)的處理流,緩沖區(qū)的作用的主要目的是:避免每次和硬盤打交道,提高數(shù)據(jù)訪問的效率。
轉(zhuǎn)化流(處理流):
InputStreamReader:把字節(jié)轉(zhuǎn)化成字符;
OutputStreamWriter:把字節(jié)轉(zhuǎn)化成字符。
基本類型數(shù)據(jù)流(處理流):用于操作基本數(shù)據(jù)類型值。
DataInputStream,
DataOutputStream。
因為平時若是我們輸出一個8個字節(jié)的long類型或4個字節(jié)的float類型,那怎么辦呢?可以一個字節(jié)一個字節(jié)輸出,也可以把轉(zhuǎn)換成字符串輸出,但是這樣轉(zhuǎn)換費時間,若是直接輸出該多好啊,因此這個數(shù)據(jù)流就解決了我們輸出數(shù)據(jù)類型的困難。數(shù)據(jù)流可以直接輸出float類型或long類型,提高了數(shù)據(jù)讀寫的效率。
打印流(處理流):
PrintStream,
PrintWriter,
一般是打印到控制臺,可以進行控制打印的地方。
對象流(處理流):
ObjectInputStream,對象反序列化;
ObjectOutputStream,對象序列化;
把封裝的對象直接輸出,而不是一個個在轉(zhuǎn)換成字符串再輸出。
合并流(處理流):
SequenceInputStream:可以認為是一個工具類,將兩個或者多個輸入流當成一個輸入流依次讀取。
1. 復制文件:
/** * 復制文件:一邊讀,一邊寫 */ class hello { public static void main(String[] args) throws IOException { if (args.length != 2) { System.out.println("命令行參數(shù)輸入有誤,請檢查"); System.exit(1); } File file1 = new File(args[0]); File file2 = new File(args[1]); if (!file1.exists()) { System.out.println("被復制的文件不存在"); System.exit(1); } InputStream input = new FileInputStream(file1); OutputStream output = new FileOutputStream(file2); if ((input != null) && (output != null)) { int temp = 0; while ((temp = input.read()) != (-1)) { output.write(temp); } } input.close(); output.close(); } }
說明:
流在使用結(jié)束后,一定要執(zhí)行關(guān)閉操作,即調(diào)用close( )方法。
FileInputStream.read():
這個方法是對這個流一個一個字節(jié)的讀,返回的結(jié)果就是這個字節(jié)的int表示方式;
當已經(jīng)沒有內(nèi)容時,返回的結(jié)果為-1;
FileOutputStream.write():
將內(nèi)容寫到文件。
2. 不使用FIle,將流中的字符轉(zhuǎn)換大寫?。?/strong>
public static void main(String[] args) throws IOException { String str = "ROLLENHOLT"; ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream output = new ByteArrayOutputStream(); int temp = 0; while ((temp = input.read()) != -1) { char ch = (char) temp; output.write(Character.toLowerCase(ch)); } String outStr = output.toString(); input.close(); output.close(); System.out.println(outStr); }
說明:
流在使用結(jié)束后,一定要執(zhí)行關(guān)閉操作,即調(diào)用close( )方法。
3. 使用管道流在多個線程間通信:
/** * 消息發(fā)送類 * */ class Send implements Runnable { private PipedOutputStream out = null; public Send() { out = new PipedOutputStream(); } public PipedOutputStream getOut() { return this.out; } public void run() { String message = "hello , Rollen"; try { out.write(message.getBytes()); } catch (Exception e) { e.printStackTrace(); } try { out.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 接受消息類 */ class Recive implements Runnable { private PipedInputStream input = null; public Recive() { this.input = new PipedInputStream(); } public PipedInputStream getInput() { return this.input; } public void run() { byte[] b = new byte[1000]; int len = 0; try { len = this.input.read(b); } catch (Exception e) { e.printStackTrace(); } try { input.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("接受的內(nèi)容為 " + (new String(b, 0, len))); } } /** * 測試類 */ class hello { public static void main(String[] args) throws IOException { Send send = new Send(); Recive recive = new Recive(); try { //管道連接 send.getOut().connect(recive.getInput()); } catch (Exception e) { e.printStackTrace(); } new Thread(send).start(); new Thread(recive).start(); } }
4. 使用緩沖區(qū)從鍵盤上讀入內(nèi)容:
public static void main(String[] args) throws IOException { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); String str = null; System.out.println("請輸入內(nèi)容"); try{ str = buf.readLine(); }catch(IOException e){ e.printStackTrace(); } System.out.println("你輸入的內(nèi)容是:" + str); }
5. 將系統(tǒng)輸出定向到文件:
public static void main(String[] args) throws IOException { File file = new File("/Users/liuluming/Documents/hello.txt"); // 此刻直接輸出到屏幕 System.out.println("hello"); try { System.setOut(new PrintStream(new FileOutputStream(file))); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("這些內(nèi)容在文件中才能看到哦!"); }
File類是對文件系統(tǒng)中文件以及文件夾進行封裝的對象,可以通過對象的思想來操作文件和文件夾。 File類保存文件或目錄的各種元數(shù)據(jù)信息,包括文件名、文件長度、最后修改時間、是否可讀、獲取當前文件的路徑名,判斷指定文件是否存在、獲得當前目錄中的文件列表,創(chuàng)建、刪除文件和目錄等方法。
該對象并不是流體系中的一員,其封裝了字節(jié)流,同時還封裝了一個緩沖區(qū)(字符數(shù)組),通過內(nèi)部的指針來操作字符數(shù)組中的數(shù)據(jù)。 該對象特點:
該對象只能操作文件,所以構(gòu)造函數(shù)接收兩種類型的參數(shù):a.字符串文件路徑;b.File對象。
該對象既可以對文件進行讀操作,也能進行寫操作,在進行對象實例化時可指定操作模式(r,rw)。
注意:
該對象在實例化時,如果要操作的文件不存在,會自動創(chuàng)建;如果文件存在,寫數(shù)據(jù)未指定位置,會從頭開始寫,即覆蓋原有的內(nèi)容。 可以用于多線程下載或多個線程同時寫數(shù)據(jù)到文件
到此,關(guān)于“Java的IO流知識點有哪些”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。