java IO流

來源:互聯網
上載者:User

標籤:The   圖片   redo   stat   str   att   強制   指定   .com   

流是一組有序的資料序列,根據操作的類型,可以分為輸入資料流和輸出資料流。

 

根據處理資料類型的不同,流又可分為位元組流和字元流。相關類位於java.io包中,以下是相關類

 1、檔案輸入\輸出資料流

位元組流:FileInputStream和FileOutputStream

FileInputStream和FileOutputStream分別繼承自InputStream和OutputStream

/**     * 寫入檔案     */    public static void testFileOutputStream(){        try {                             File file=new File("D:\\test.txt");                if(!file.exists()){                     file.createNewFile();                }                FileOutputStream fileOutputStream=new FileOutputStream(file);                String str="this is the content";                byte[] b=str.getBytes();                fileOutputStream.write(b);                fileOutputStream.close();              } catch (IOException e) {                       e.printStackTrace();        }    }    /**     * 讀檔案     */    public static void testFileInputStream(){        try {                             File file=new File("D:\\test.txt");                if(!file.exists()){                     file.createNewFile();                }                FileInputStream fileInputStream=new FileInputStream(file);                  byte[] b=new byte[1024];                 System.out.println("b before:"+new String(b));                int len=fileInputStream.read(b);//Reads up to b.length bytes of data from this input stream into an array of bytes.                 System.out.println("b after:"+new String(b,0,len));                fileInputStream.close();              } catch (IOException e) {                   e.printStackTrace();        }    }

輸出:

b before:b after:this is the content

可見,FileInputStream的read(byte[] b)方法是從流中讀取b中(Reads up to b.length bytes of data from this input stream into an array of bytes. )

 

字元流:FileReader和FileWriter

FileReader和FileWriter分別繼承自Reader和Writer

/**     * 寫入檔案     */    public static void testFileWriter(){        try {                             File file=new File("D:\\test.txt");                if(!file.exists()){                     file.createNewFile();                }                FileWriter fileWriter=new FileWriter(file);                String str="this is the content2";                fileWriter.write(str);                fileWriter.close();              } catch (IOException e) {                       e.printStackTrace();        }    }        /**     * 讀檔案     */    public static void testFileReader(){        try {                             File file=new File("D:\\test.txt");                if(!file.exists()){                     file.createNewFile();                }                FileReader fileReader=new FileReader(file);                  char[] cbuf=new char[1024];                 System.out.println("cbuf before:"+new String(cbuf));                int len=fileReader.read(cbuf);//Attempts to read characters into the specified character buffer.                 System.out.println("cbuf after:"+new String(cbuf,0,len));                fileReader.close();              } catch (IOException e) {                   e.printStackTrace();        }    }

輸出:

cbuf before:cbuf after:this is the content2

FileReader的read(char[] cbuf)方法,是將流的內容讀取到cbuf中

2、帶緩衝的輸入/輸出流

緩衝是I/O的一種效能最佳化,緩衝流為I/O流增加了記憶體緩衝區。有了緩衝區,使得在流上執行skip()、mark()和reset()方法成為可能。

位元組流:BufferedInputStream和BufferedOutputStream

BufferedInputStream對所有InputStream類進行帶緩衝區的封裝以達到效能的最佳化。BufferedInputStream有兩個構造方法:

(1) BufferedInputStream(InputStream in)

(2) BufferedInputStream(InputStream in,int size)

第一個構造方法建立了一個帶有32位元組的緩衝流;第二個構造方法size參數指定了建立的緩衝區的大小。

BufferedInputStream讀取檔案的流程如下:

 

BufferedOutputStream與BufferedInputStream類似,也有兩個構造方法,

(1) BufferedOutputStream(OutputStream out)

(2) BufferedOutputStream(OutputStream out,int size)

第一個構造方法建立了一個帶有32位元組的緩衝流;第二個構造方法size參數指定了建立的緩衝區的大小。

BufferedOutputStream的flush()方法強制將緩衝區的資料輸出完。

以下是代碼:

/**     * 寫入檔案     */    public static void testBufferedOutputStream(){        try {                             File file=new File("D:\\test.txt");                if(!file.exists()){                     file.createNewFile();                }                FileOutputStream fileOutputStream=new FileOutputStream(file);                BufferedOutputStream BufferedOutputStream=new BufferedOutputStream(fileOutputStream);                             String str="this is the content3";                byte[] b=str.getBytes();                BufferedOutputStream.write(b);                                BufferedOutputStream.close();                   fileOutputStream.close();        } catch (IOException e) {                       e.printStackTrace();        }    }    /**     * 讀檔案     */    public static void testBufferedInputStream(){        try {                             File file=new File("D:\\test.txt");                if(!file.exists()){                     file.createNewFile();                }                FileInputStream fileInputStream=new FileInputStream(file);                BufferedInputStream BufferedInputStream=new BufferedInputStream(fileInputStream);                  byte[] b=new byte[1024];                 System.out.println("b before:"+new String(b));                int len=BufferedInputStream.read(b);//Reads up to byte.length bytes of data from this input stream into an array of bytes.                 System.out.println("b after:"+new String(b,0,len));                  BufferedInputStream.close();                     fileInputStream.close();        } catch (IOException e) {                   e.printStackTrace();        }    }

 

字元流:BufferedReader和BufferedWriter

BufferedReader和BufferedWriter分別繼承自Reader和Writer,具有內部緩衝的機制,並可以以行為單位進行輸入輸出。是讀取的過程

以下是程式碼範例:

/**     * 寫檔案     */    public static void testBufferedWriter(){        try {                             File file=new File("D:\\test.txt");                if(!file.exists()){                     file.createNewFile();                }                FileOutputStream fileOutputStream=new FileOutputStream(file);                OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream);                BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);                             String str="this is the content5\n thissssss";                bufferedWriter.write(str);                bufferedWriter.close();                outputStreamWriter.close();                   fileOutputStream.close();        } catch (IOException e) {                       e.printStackTrace();        }    }    /**     * 讀檔案     */    public static void testBufferedReader(){        try {                             File file=new File("D:\\test.txt");                if(!file.exists()){                     file.createNewFile();                }                FileInputStream fileInputStream=new FileInputStream(file);                InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);                BufferedReader bufferedReader=new BufferedReader(inputStreamReader);                                             String str=bufferedReader.readLine();                System.out.println(str);                int i=bufferedReader.read();                System.out.println(i);                                bufferedReader.close();                inputStreamReader.close();                   fileInputStream.close();        } catch (IOException e) {                       e.printStackTrace();        }    }

輸出結果:

this is the content532

 

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.