java之IO位元組流相關操作

來源:互聯網
上載者:User

標籤:讀取資料   height   start   string   lin   流程式控制制   demo   ati   識別   

File對象可以表示存在的檔案或檔案夾,也可以表示不存在的。我們想要得到檔案的內容怎麼辦,File只是操作檔案,檔案的內容如何處理就需要使用io流技術了。例如在C盤下有一個名稱為a.txt的文字檔.想要通過Java程式讀出來檔案中的內容,需要使用IO流技術.同樣想要將程式中的資料,儲存到硬碟的檔案中,也需要IO流技術.

 

IO流簡介:(Input/Output)

I/O類庫中使用“流”這個抽象概念。Java對裝置中資料的操作是通過流的方式。表示任何有能力產出資料的資料來源對象,或者是有能力接受資料的接收端對象。“流”屏蔽了實際的I/O裝置中處理資料的細節。IO流用來處理裝置之間的資料轉送。裝置是指硬碟、記憶體、鍵盤錄入、網路等。Java用於操作流的對象都在IO包中。IO流技術主要用來處理裝置之間的資料轉送。由於Java用於操作流的對象都在IO包中。所以使用IO流需要導包如:import java.io.*;

IO流的分類

位元組流: 位元組流讀取得都是檔案中位元據,讀取到位元據不會經過任何的處理。

字元流: 字元流讀取的資料是以字元為單位的 。 字元流也是讀取檔案中的位元據,不過會把這些位元據轉換成我們能 識別的字元。 字元流 = 位元組流 + 解碼



輸入位元組流:
--------| InputStream 所有輸入位元組流的基類 抽象類別
------------| FileInputStream 讀取檔案資料的輸入位元組流
四種方式讀取檔案內容:
使用FileInputStream讀取檔案資料的步驟:
1. 找到目標檔案
2. 建立資料的輸入通道。
3. 讀取檔案中的資料。
4. 關閉 資源.

 方法一:

public static void readFile01() throws IOException {//不能讀到完整的檔案內容            File file=new File("D:\\java\\傳智java\\day03\\day03流程式控制制語句.doc");//找到目標檔案            FileInputStream fileInputStream=new FileInputStream(file);//建立檔案輸入通道            int count =fileInputStream.read();//讀取檔案。            System.out.println(count);            fileInputStream.close();//關閉資源        }

 

此方法確定:只能讀取一個位元組,不能讀取完整的資料。

將上述方法進行最佳化,FileInputStream的read方法當位元組已經到達末尾處時,就不進行讀取了。

方法二:

import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class Demo03 {    public static void main(String[] args) throws IOException {        readFile();    }    public static void readFile() throws IOException{        File file =new File("C:\\a.txt");        FileInputStream fileInputStream=new FileInputStream(file);        int conent=0;        while ((conent=fileInputStream.read())!=-1) {            System.out.print((char)conent);        }
     fileInputStream.close(); }}

當位元組已經到達末尾處時,就不進行讀取了。

讀取的內容是:abc

方式三:

//方式3:使用緩衝 數組 讀取。    缺點: 無法讀取完整一個檔案的資料。        public static void readFile3() throws IOException{        //找到目標檔案        File file = new File("C:\\a.txt");        //建立資料的輸入通道        FileInputStream fileInputStream = new FileInputStream(file);        //建立緩衝位元組數組,讀取檔案的資料。        byte[] buf = new byte[1024];  //相當於超市裡面的購物車。當檔案大於1024位元組時,後面的內容就無法擷取了。        int length = fileInputStream.read(buf); // 如果使用read讀取資料傳入位元組數組,那麼資料是儲存到位元組數組中的,而這時
                            //候read方法的傳回值是表示的是本次讀取了幾個位元組資料到位元組數組中。
System.out.println("length:"+ length); //使用位元組數組構建字串 String content = new String(buf,0,length); System.out.println("內容:"+ content); //關閉資源 fileInputStream.close(); }

方式4:

//方式4:使用緩衝數組配合迴圈一起讀取。28    public static void readTest4() throws IOException{        long startTime = System.currentTimeMillis();        //找到目標檔案        File file = new File("C:\\a.txt");        //建立資料的輸入通道        FileInputStream fileInputStream = new FileInputStream(file);        //建立緩衝數組配合迴圈讀取檔案的資料。        int length = 0; //儲存每次讀取到的位元組個數。        byte[] buf = new byte[1024]; //儲存讀取到的資料    緩衝數組 的長度一般是1024的倍數,因為與電腦的處理單位。  理論上緩衝數組越大,效率越高        while((length = fileInputStream.read(buf))!=-1){ // read方法如果讀取到了檔案的末尾,那麼會返回-1表示。            System.out.print(new String(buf,0,length));        }        //關閉資源        fileInputStream.close();        long endTime = System.currentTimeMillis();        System.out.println("讀取的時間是:"+ (endTime-startTime)); //446    }

 一般使用第四種方式讀取,效率比較高,既能讀取完整資料,又能將內容儲存到緩衝資料。

java之IO位元組流相關操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.