Java語言中的IO系統(tǒng)怎么使用

本篇內(nèi)容介紹了“Java 語言中的IO系統(tǒng)怎么使用”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、成都做網(wǎng)站、東昌府網(wǎng)絡(luò)推廣、小程序定制開發(fā)、東昌府網(wǎng)絡(luò)營銷、東昌府企業(yè)策劃、東昌府品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供東昌府建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:bm7419.com

Input和Output

1. stream代表的是任何有能力產(chǎn)出數(shù)據(jù)的數(shù)據(jù)源,或是任何有能力接收數(shù)據(jù)的接收源。在Java的IO系統(tǒng)中,所有的stream(包括Input和Out stream)都包括兩種類型:

1.1 以字節(jié)為導(dǎo)向的stream

以字節(jié)為導(dǎo)向的stream,表示以字節(jié)為單位從stream中讀取或往stream中寫入信息。以字節(jié)為導(dǎo)向的stream包括下面幾種類型:

input

stream:

1) ByteArrayInputStream:把內(nèi)存中的一個緩沖區(qū)作為InputStream使用

2) StringBufferInputStream:把一個String對象作為InputStream

3) FileInputStream:把一個文件作為InputStream,實現(xiàn)對文件的讀取操作

4) PipedInputStream:實現(xiàn)了pipe的概念,主要在線程中使用

5) SequenceInputStream:把多個InputStream合并為一個InputStream

Out

stream

1) ByteArrayOutputStream:把信息存入內(nèi)存中的一個緩沖區(qū)中

2) FileOutputStream:把信息存入文件中

3) PipedOutputStream:實現(xiàn)了pipe的概念,主要在線程中使用

4) SequenceOutputStream:把多個OutStream合并為一個OutStream

1.2 以Unicode字符為導(dǎo)向的stream

以Unicode字符為導(dǎo)向的stream,表示以Unicode字符為單位從stream中讀取或往stream中寫入信息。以Unicode字符為導(dǎo)向的stream包括下面幾種類型:

Input

Stream

1) CharArrayReader:與ByteArrayInputStream對應(yīng)

2) StringReader:與StringBufferInputStream對應(yīng)

3) FileReader:與FileInputStream對應(yīng)

4) PipedReader:與PipedInputStream對應(yīng)

Out

Stream

1) CharArrayWrite:與ByteArrayOutputStream對應(yīng)

2) StringWrite:無與之對應(yīng)的以字節(jié)為導(dǎo)向的stream

3) FileWrite:與FileOutputStream對應(yīng)

4) PipedWrite:與PipedOutputStream對應(yīng)

以字符為導(dǎo)向的stream基本上對有與之相對應(yīng)的以字節(jié)為導(dǎo)向的stream。兩個對應(yīng)類實現(xiàn)的功能相同,字是在操作時的導(dǎo)向不同。如 CharArrayReader:和ByteArrayInputStream的作用都是把內(nèi)存中的一個緩沖區(qū)作為InputStream使用,所不同的 是前者每次從內(nèi)存中讀取一個字節(jié)的信息,而后者每次從內(nèi)存中讀取一個字符。

1.3 兩種不現(xiàn)導(dǎo)向的stream之間的轉(zhuǎn)換

InputStreamReader和OutputStreamReader:把一個以字節(jié)為導(dǎo)向的stream轉(zhuǎn)換成一個以字符為導(dǎo)向的stream。

2. stream添加屬性

2.1 “為stream添加屬性”的作用

運用上面介紹的Java中操作IO的API,我們就可完成我們想完成的任何操作了。但通過FilterInputStream和FilterOutStream的子類,我們可以為stream添加屬性。下面以一個例子來說明這種功能的作用。

如果我們要往一個文件中寫入數(shù)據(jù),我們可以這樣操作:

FileOutStream fs = new FileOutStream(“test.txt”);

然后就可以通過產(chǎn)生的fs對象調(diào)用write()函數(shù)來往test.txt文件中寫入數(shù)據(jù)了。但是,如果我們想實現(xiàn)“先把要寫入文件的數(shù)據(jù)先緩存到內(nèi)存 中,再把緩存中的數(shù)據(jù)寫入文件中”的功能時,上面的API就沒有一個能滿足我們的需求了。但是通過FilterInputStream和 FilterOutStream的子類,為FileOutStream添加我們所需要的功能。

2.2 FilterInputStream的各種類型

2.2.1 用于封裝以字節(jié)為導(dǎo)向的InputStream

1) DataInputStream:從stream中讀取基本類型(int、char等)數(shù)據(jù)。

2) BufferedInputStream:使用緩沖區(qū)

3) LineNumberInputStream:會記錄input stream內(nèi)的行數(shù),然后可以調(diào)用getLineNumber()和setLineNumber(int)

4) PushbackInputStream:很少用到,一般用于編譯器開發(fā)

2.2.2 用于封裝以字符為導(dǎo)向的InputStream

1) 沒有與DataInputStream對應(yīng)的類。除非在要使用readLine()時改用BufferedReader,否則使用DataInputStream

2) BufferedReader:與BufferedInputStream對應(yīng)

3) LineNumberReader:與LineNumberInputStream對應(yīng)

4) PushBackReader:與PushbackInputStream對應(yīng)

2.3 FilterOutStream的各種類型

2.2.3 用于封裝以字節(jié)為導(dǎo)向的OutputStream

1) DataIOutStream:往stream中輸出基本類型(int、char等)數(shù)據(jù)。

2) BufferedOutStream:使用緩沖區(qū)

3) PrintStream:產(chǎn)生格式化輸出

2.2.4 用于封裝以字符為導(dǎo)向的OutputStream

1) BufferedWrite:與對應(yīng)

2) PrintWrite:與對應(yīng)

3. RandomAccessFile

1) 可通過RandomAccessFile對象完成對文件的讀寫操作

2) 在產(chǎn)生一個對象時,可指明要打開的文件的性質(zhì):r,只讀;w,只寫;rw可讀寫

3) 可以直接跳到文件中指定的位置

4. I/O應(yīng)用的一個例子

java 代碼

import java.io.*;   public class TestIO{    public static void main(String[] args)    throws IOException{    //1.以行為單位從一個文件讀取數(shù)據(jù)    BufferedReader in = new BufferedReader(   new FileReader("F:\\nepalon\\TestIO.java"));    String s, s2 = new String();    while((s = in.readLine()) != null)    s2 += s + "\n";    in.close();    //1b. 接收鍵盤的輸入    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));    System.out.println("Enter a line:");    System.out.println(stdin.readLine());    //2. 從一個String對象中讀取數(shù)據(jù)    StringReader in2 = new StringReader(s2);    int c;    while((c = in2.read()) != -1)    System.out.println((char)c);    in2.close();    //3. 從內(nèi)存取出格式化輸入    try{   DataInputStream in3 =new DataInputStream(new ByteArrayInputStream(s2.getBytes()));   while(true)    System.out.println((char)in3.readByte());    }    catch(EOFException e){   System.out.println("End of stream");    }    //4. 輸出到文件    try{   BufferedReader in4 =new BufferedReader(new StringReader(s2));   PrintWriter out1 =new PrintWriter(new BufferedWriter(new FileWriter("F:\\nepalon\\ TestIO.out")));   int lineCount = 1;   while((s = in4.readLine()) != null)    out1.println(lineCount++ + ":" + s);    out1.close();    in4.close();    }    catch(EOFException ex){   System.out.println("End of stream");    }    //5. 數(shù)據(jù)的存儲和恢復(fù)    try{   DataOutputStream out2 =new DataOutputStream(new BufferedOutputStream(    new FileOutputStream("F:\\nepalon\\ Data.txt")));   out2.writeDouble(3.1415926);   out2.writeChars("\nThas was pi:writeChars\n");   out2.writeBytes("Thas was pi:writeByte\n");   out2.close();   DataInputStream in5 =new DataInputStream(    new BufferedInputStream(new FileInputStream("F:\\nepalon\\ Data.txt")));    BufferedReader in5br =new BufferedReader(new InputStreamReader(in5));    System.out.println(in5.readDouble());    System.out.println(in5br.readLine());    System.out.println(in5br.readLine());    }    catch(EOFException e){   System.out.println("End of stream");    }    //6. 通過RandomAccessFile操作文件    RandomAccessFile rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");    for(int i=0; i <10; i++)   rf.writeDouble(i*1.414);   rf.close();   rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");   for(int i=0; i <10; i++)    System.out.println("Value " + i + ":" + rf.readDouble());    rf.close();    rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");    rf.seek(5*8);    rf.writeDouble(47.0001);    rf.close();    rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");    for(int i=0; i <10; i++)   System.out.println("Value " + i + ":" + rf.readDouble());   rf.close();   }    }

關(guān)于代碼的解釋(以區(qū)為單位):

1區(qū)中,當讀取文件時,先把文件內(nèi)容讀到緩存中,當調(diào)用in.readLine()時,再從緩存中以字符的方式讀取數(shù)據(jù)(以下簡稱“緩存字節(jié)讀取方式”)。

1b區(qū)中,由于想以緩存字節(jié)讀取方式從標準IO(鍵盤)中讀取數(shù)據(jù),所以要先把標準IO(System.in)轉(zhuǎn)換成字符導(dǎo)向的stream,再進行BufferedReader封裝。

2區(qū)中,要以字符的形式從一個String對象中讀取數(shù)據(jù),所以要產(chǎn)生一個StringReader類型的stream。

“Java 語言中的IO系統(tǒng)怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

名稱欄目:Java語言中的IO系統(tǒng)怎么使用
鏈接URL:http://bm7419.com/article20/gipjco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、App開發(fā)、網(wǎng)頁設(shè)計公司、網(wǎng)站排名、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)