IO【位元組流、高效流】,io位元組流

來源:互聯網
上載者:User

IO【位元組流、高效流】,io位元組流

IO流:
I:input,輸入,讀取到記憶體
O:Output,輸出,寫入檔案
流:資料流(字元,位元組)
分類:
流向:
輸入:位元組輸入資料流(FileInputStream),字元輸入資料流(FileReader)
輸出:位元組輸出資料流(FileOutputStream),字元輸入資料流(FileWriter)
種類:
字元,位元組

java.io.OutputStream:位元組輸出資料流,是所有位元組輸出資料流的父類

公用的成員方法:
abstract void write(int b) 寫入一個位元組
void write(byte[] b) 寫入位元組數組
void write(byte[] b, int off, int len) 寫入位元組數組,off是開始的索引,len寫幾個
void close() 關閉此輸出資料流並釋放與此流有關的所有系統資源。

java.io.FileOutputStream:檔案位元組輸出資料流
作用:把記憶體中的資料,以位元組的方式,寫入到檔案中

構造方法:
FileOutputStream(File file) 建立一個向指定 File 對象表示的檔案中寫入資料的檔案輸出資料流。
FileOutputStream(String name) 建立一個向具有指定名稱的檔案中寫入資料的輸出檔案流。
構造方法的參數:
file:檔案
name:檔案的路徑
都是寫入資料的目的地


特點:

如果構造方法中指定的檔案或者檔案的路徑所指向的檔案不存在,構造方法就會建立一個檔案
如果,沒有開啟追加寫的開關,檔案已經存在,則會覆蓋


使用步驟:
1.建立一個位元組輸出資料流對象FileOutputStream,綁定資料的目的地
2.使用FileOutputStream中的write方法,把資料寫入到檔案中
3.釋放資源

流寫入資料的時候,會找到JVM,JVM會調用系統本地的方法完成寫,使用完 流要使用和系統有關的資源(釋放記憶體)

void write(byte[] b) 寫入位元組數組
void write(byte[] b, int off, int len) 寫入位元組數組,off是開始的索引,len寫幾個

 1 public static void main(String[] args) throws IOException { 2         File file = new File("b.txt"); 3         FileOutputStream fos = new FileOutputStream(file); 4         //寫入100到檔案中,100是3個位元組 5         fos.write(49); 6         fos.write(48); 7         fos.write(48); 8          9         /*10          * void write(byte[] b) 寫入位元組數組11          * 寫入資料的時候,如果一次寫多個位元組12          * 寫入的位元組是正數:會查詢ASC||表13          * 寫入的位元組是,第一個位元組是負數,第二個位元組可以是正數,也可以是負數,查詢的時候就會把兩個位元組組成一個中文,查詢GBK編碼錶14          */15         byte[] bytes = {65,66,67,68,69};//ABCDE16         //byte[] bytes = {-65,-66,-67,68,88};//烤紻17         18         fos.write(bytes);19         20         //void write(byte[] b, int off, int len) 寫入位元組數組,off是開始的索引,len寫幾個21         fos.write(bytes, 1, 2);22         23         /*24          * 快速寫入位元組數組的方法25          * String類中有一個方法26          * byte[] getBytes(String charsetName) :把字串轉換為位元組數組 27          */28         byte[] bytes2 = "你好".getBytes();29         System.out.println(Arrays.toString(bytes2));//[-60, -29, -70, -61]30         fos.write(bytes2);31         32         fos.close();33     }

void write(byte[] b) 寫入位元組數組
寫入資料的時候,如果一次寫多個位元組
寫入的位元組是正數:會查詢ASC||表
寫入的位元組是,第一個位元組是負數,第二個位元組可以是正數,也可以是負數,查詢的時候就會把兩個位元組組成一個中文,查詢GBK編碼錶

快速寫入位元組數組的方法
String類中有一個方法
byte[] getBytes(String charsetName) :把字串轉換為位元組數組

檔案的續寫和換行:
換行:
windows:\r\n
linux:\n
mac:\r
追加寫:使用兩個參數的構造方法
FileOutputStream(File file, boolean append)
FileOutputStream(String name, boolean append)
參數:
File file,String name:寫入資料的目的地
boolean append:追加寫的開關,true:可以追加寫(往之前的檔案,繼續寫內容),fasle:不能追加寫(覆蓋之前的檔案)

java.io.InputStream:位元組輸入資料流,是所有位元組輸入資料流的父類

公用的成員方法:
int read():讀取一個位元組並返回,沒有位元組返回-1.
int read(byte[]): 讀取一定量的位元組數,並儲存到位元組數組中,返回讀取到的位元組數。
void close() 關閉此檔案輸入資料流並釋放與此流有關的所有系統資源。

java.io.FileInputStream:檔案位元組輸入資料流
作用:把檔案中的資料,以位元組的方式讀取到記憶體中

構造方法:
FileInputStream(String name)
FileInputStream(File file)
參數:讀取的哪個檔案(資料來源)
String name:字串的檔案路徑
File file:讀取的檔案

使用步驟:
1.建立位元組輸入資料流對象FileInputStream,並且綁定資料來源
2.使用FileInputStream對象中的方法read,讀取檔案
3.釋放資源


int read(byte[]): 讀取一定量的位元組數,並儲存到位元組數組中,返回讀取到的位元組數。
明確:
1.byte數組的作用:起到緩衝作用,一次可以往數組中緩衝多個位元組,可以提高讀取效率
byte數組的長度:一般定義為1024(一kb位元組)或者1024的整數倍
2.傳回值int是什麼:每次讀取的有效位元組個數

檔案的複製:讀取檔案採用一次讀取一個位元組的方式,寫入檔案採用一次寫一個位元組的方式

資料來源:c:\\1.jpg
資料目的地:d:\\1.jpg

操作步驟:
1.建立位元組輸入資料流對象FileInputStream,並且綁定資料來源
2.建立位元組輸出資料流對象FileOutputStream,並且綁定資料目的地
3.使用FileInputStream中的方法read,一次讀取一個位元組
4.使用FileOutputStream中的方法write,一次寫一個位元組
5.釋放資源(先關寫,後關讀)

 1 public static void main(String[] args) throws IOException { 2         long s = System.currentTimeMillis(); 3         //1.建立位元組輸入資料流對象FileInputStream,並且綁定資料來源 4         FileInputStream fis = new FileInputStream("c:\\1.jpg"); 5         //2.建立位元組輸出資料流對象FileOutputStream,並且綁定資料目的地 6         FileOutputStream fos = new FileOutputStream("d:\\1.jpg"); 7         //3.使用FileInputStream中的方法read,一次讀取一個位元組 8         int len = 0;//接收讀取到的位元組 9         while((len = fis.read())!=-1){10             //4.使用FileOutputStream中的方法write,一次寫一個位元組11             fos.write(len);12         }13         //5.釋放資源(先關寫,後關讀)14         fos.close();15         fis.close();16         long e = System.currentTimeMillis();17         System.out.println(e-s);18     }    

檔案的複製:讀取檔案採用位元組數組緩衝讀取,寫入資料一次寫入多個位元組
*
* 資料來源:c:\\1.jpg
* 資料目的地:d:\\1.jpg
*
* 操作步驟:
* 1.建立位元組輸入資料流對象FileInputStream,並且綁定資料來源
* 2.建立位元組輸出資料流對象FileOutputStream,並且綁定資料目的地
* 3.使用FileInputStream中的方法read(byte[]),一次讀取多個位元組
* 4.使用FileOutputStream中的方法write(byte[],0,len),一次寫多個位元組
* 5.釋放資源

 1 public static void main(String[] args) throws Exception { 2         long s = System.currentTimeMillis(); 3         //1.建立位元組輸入資料流對象FileInputStream,並且綁定資料來源 4         FileInputStream fis =  new FileInputStream("c:\\z.zip"); 5         //2.建立位元組輸出資料流對象FileOutputStream,並且綁定資料目的地 6         FileOutputStream fos = new FileOutputStream("d:\\z.zip"); 7         //3.使用FileInputStream中的方法read(byte[]),一次讀取多個位元組 8         byte[] bytes = new byte[1024*100]; 9         int len = 0;//讀取的位元組有效個數10         while((len = fis.read(bytes))!=-1){11             //4.使用FileOutputStream中的方法write(byte[],0,len),一次寫多個位元組12             fos.write(bytes, 0, len);13         }14         //5.釋放資源15         fos.close();16         fis.close();17         18         long e = new Date().getTime();19         System.out.println(e-s);20     }

檔案的複製:讀取檔案使用緩衝流+數組,寫入資料使用緩衝流一次寫入多個
*
* 資料來源:c:\\1.jpg
* 資料目的地:d:\\1.jpg
*
* 操作步驟:
* 1.建立FileInputStream對象,綁定資料來源
* 2.建立BufferedInputStream對象,構造方法中傳入FileInputStream,提高FileInputStream的讀取效率
* 3.建立FileOutputStream對象,綁定資料目的地
* 4.建立BufferedOutputStream對象,構造方法中傳遞FileOutputStream,提高FileOutputStream效率
* 5.使用BufferedInputStream中的方法read(byte[]),讀取檔案
* 6.使用BufferedOutputStream中的方法write(byte[],0,len),寫入資料到緩衝區
* 7.使用BufferedOutputStream中的方法flush把緩衝區的資料,重新整理到檔案中
* 8.釋放資源

 1 public static void main(String[] args) throws Exception { 2         long s = System.currentTimeMillis(); 3         //1.建立FileInputStream對象,綁定資料來源 4         FileInputStream fis = new FileInputStream("c:\\z.zip"); 5         //2.建立BufferedInputStream對象,構造方法中傳入FileInputStream 6         BufferedInputStream bis = new BufferedInputStream(fis); 7         //3.建立FileOutputStream對象,綁定資料目的地 8         FileOutputStream fos = new FileOutputStream("d:\\z.zip"); 9         //4.建立BufferedOutputStream對象,構造方法中傳遞FileOutputStream,提高FileOutputStream效率10         BufferedOutputStream bos = new BufferedOutputStream(fos);11         //5.使用BufferedInputStream中的方法read(byte[]),讀取檔案12         /*int len = 0;13         while((len = bis.read())!=-1){14             //6.使用BufferedOutputStream中的方法write(byte[],0,len),寫入資料到緩衝區15             bos.write(len);16         }*/17         byte[] bytes = new byte[1024*100];18         int len = 0;19         while((len = bis.read(bytes))!=-1){20             fos.write(bytes, 0, len);21             fos.flush();22         }23         24         //8.釋放資源25         bos.close();26         bis.close();27         28         long e = new Date().getTime();29         System.out.println(e-s);30     }

java.io.BufferedOutputStream:位元組緩衝輸出資料流 extends OutputStream
* 位元組緩衝輸出資料流作用:給基本流增加一個緩衝區,提高基本流的效率
*
* 繼承自父類的公用的成員方法
* abstract void write(int b) 寫入一個位元組
* void write(byte[] b) 寫入位元組數組
* void write(byte[] b, int off, int len) 寫入位元組數組,off是開始的索引,len寫幾個
* void close() 關閉此輸出資料流並釋放與此流有關的所有系統資源。
*
* 構造方法:
* BufferedOutputStream(OutputStream out) 建立一個新的緩衝輸出資料流,以將資料寫入指定的底層輸出資料流。
* 參數:
* OutputStream out:位元組輸出資料流,可以使用FileOutputStream
* 參數傳遞時哪個位元組輸出資料流,就會給哪個位元組輸出資料流增加一個緩衝區,提高這個流的效率
* 使用步驟:
* 1.建立FileOutputStream對象,綁定資料目的地
* 2.建立BufferedOutputStream對象,構造方法中傳遞FileOutputStream,提高FileOutputStream效率
* 3.使用BufferedOutputStream中的方法write,把資料寫入到緩衝區中
* 4.使用BufferedOutputStream中的方法flush,把緩衝區中的資料,重新整理到檔案中
* 5.釋放資源

 1  public static void main(String[] args) throws IOException { 2         //1.建立FileOutputStream對象,綁定資料目的地 3         FileOutputStream fos = new FileOutputStream("buffered.txt"); 4         //2.建立BufferedOutputStream對象,構造方法中傳遞FileOutputStream 5         BufferedOutputStream bos = new BufferedOutputStream(fos); 6         //3.使用BufferedOutputStream中的方法write,把資料寫入到緩衝區中 7         bos.write(97); 8          9         bos.write("我是緩衝流".getBytes());10         //4.使用BufferedOutputStream中的方法flush,把緩衝區中的資料,重新整理到檔案中11         bos.flush();12         //5.釋放資源13         bos.close();14     }

java.io.BufferedInputStream:位元組緩衝輸入資料流 extends InputStream
* 作用:給基本位元組輸入資料流增加一個緩衝區,提高基本位元組輸入資料流的效率
*
* 繼承自父類的公用發的成員方法:
* int read():讀取一個位元組並返回,沒有位元組返回-1.
* int read(byte[]): 讀取一定量的位元組數,並儲存到位元組數組中,返回讀取到的位元組數。
* void close() 關閉此檔案輸入資料流並釋放與此流有關的所有系統資源。
*
* 構造方法:
* BufferedInputStream(InputStream in) 建立一個 BufferedInputStream 並儲存其參數,即輸入資料流 in,以便將來使用。
* 參數:
* InputStream in:位元組輸入資料流,可以傳遞FileInputStream
* 參數傳遞的是哪個位元組輸入資料流對象,就會給哪個位元組輸入資料流對象增加一個緩衝區,提高該流的效率
*
* 使用步驟:
* 1.建立FileInputStream對象,綁定資料來源
* 2.建立BufferedInputStream對象,構造方法中傳入FileInputStream,提高FileInputStream的讀取效率
* 3.使用BufferedInputStream中的方法read,讀取檔案
* 4.釋放資源
*
* 總結:
* 位元組流:操作的檔案是非文字檔,檔案的複製
* 字元流:操作的檔案都是文字檔,一次讀取一個字元,可以讀取中文
* 文字檔:使用記事本開啟,能看懂

 1 public static void main(String[] args) throws IOException { 2         //1.建立FileInputStream對象,綁定資料來源 3         FileInputStream fis = new FileInputStream("buffered.txt"); 4         //2.建立BufferedInputStream對象,構造方法中傳入FileInputStream 5         BufferedInputStream bis = new BufferedInputStream(fis); 6         //3.使用BufferedInputStream中的方法read,讀取檔案 7         //int read():讀取一個位元組並返回,沒有位元組返回-1. 8         /*int len = 0; 9         while((len = bis.read())!=-1){10             System.out.println((char)len);11         }*/12         13         //int read(byte[]): 讀取一定量的位元組數,並儲存到位元組數組中,返回讀取到的位元組數。14         byte[] bytes = new byte[1024];15         int len = 0;16         while((len = bis.read(bytes))!=-1){17             System.out.println(new String(bytes,0,len));            18         }19         20         //4.釋放資源21         bis.close();22         System.out.println("-------------------");23         BufferedReader br = new BufferedReader(new FileReader("buffered.txt"));24         while((len = br.read())!=-1){25             System.out.println((char)len);26         }27     }

複製單層檔案夾
*
* 資料來源:c:\\demo
* 資料目的地:d:\\
*
* 操作流程:
* 1.判斷d盤有沒有demo檔案夾,沒有則建立
* 2.建立一個複製檔案的方法
* 傳回值類型:void
* 方法名:copyFile
* 參數列表:File src,File dest
* 3.遍曆要複製的檔案夾,擷取檔案夾中的每一個檔案的路徑(要複製檔案的資料來源)
* 4.使用file類中的方法getName拼接要複製檔案的資料目的
* 5.調用copyFile複製的方法進行複製

 1 public static void main(String[] args) throws IOException { 2         //1.判斷d盤有沒有demo檔案夾,沒有則建立 3         File file = new File("d:\\demo"); 4         if(!file.exists()){ 5             file.mkdirs(); 6         } 7         //3.遍曆要複製的檔案夾,擷取檔案夾中的每一個檔案的路徑(要複製檔案的資料來源) 8         File srcDirectory = new File("c:\\demo"); 9         File[] srcFiles = srcDirectory.listFiles();10         for (File srcFile : srcFiles) {11             //4.使用file類中的方法getName拼接要複製檔案的資料目的12             String srcName = srcFile.getName();13             //使用File類的第三個構造方法建立目的地14             File destFile = new File(file, srcName);15             //5.調用copyFile複製的方法進行複製16             copyFile(srcFile, destFile);17         }18     }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.