1. 程式人生 > >Java IO流

Java IO流

io 流 inputstream outputstream

Java中的流,可以從不同的角度進行分類。

按照數據流的方向不同可以分為:輸入流和輸出流。

按照處理數據單位不同可以分為:字節流和字符流。

按照實現功能不同可以分為:節點流和處理流。

輸出流:

技術分享

輸入流:

技術分享

因此輸入和輸出都是從程序的角度來說的。

字節流:一次讀入或讀出是8位二進制。

字符流:一次讀入或讀出是16位二進制。

字節流和字符流的原理是相同的,只不過處理的單位不同而已。後綴是Stream是字節流,而後綴是ReaderWriter是字符流。

節點流:直接與數據源相連,讀入或讀出。

技術分享

直接使用節點流,讀寫不方便,為了更快的讀寫文件,才有了處理流。

處理流:與節點流一塊使用,在節點流的基礎上,再套接一層,套接在節點流上的就是處理流。

技術分享

Jdk提供的流繼承了四大類:InputStream(字節輸入流)OutputStream(字節輸出流),Reader(字符輸入流),Writer(字符輸出流)。

以下是javaio中常用的流。

技術分享

字節輸入流:

技術分享

字節輸出流:

技術分享

字符輸入流:

技術分享

字符輸出流:

技術分享

簡單介紹其上圖:

對文件進行操作:FileInputStream(字節輸入流),FileOutputStream(字節輸出流),FileReader(字符輸入流),FileWriter(字符輸出流)

FileReader、FileWriter 可以實現文本文件的復制, 對於非文本文件(視頻文件、音頻文件、圖片),只能使用字節流!

例子:

// 字節流實現文件復制的方法
    public void copyFile(String src, String dest) {
        // 1.提供讀入、寫出的文件
        File file1 = new File(src);
        File file2 = new File(dest);
        // 2.提供相應的流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            // 3.每次讀取的字節數
            byte[] b = new byte[20];
            int len;
            while ((len = fis.read(b)) != -1) {
                // fos.write(b);//錯誤的寫法兩種: fos.write(b,0,b.length);
                fos.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
// 字符流實現文件復制的方法
    public void copyFile(String src, String dest) {
        //1.輸入流對應的文件src一定要存在,否則拋異常。輸出流對應的文件dest可以不存在,執行過程中會自動創建
        FileReader fr = null;
        FileWriter fw = null;
        // 1.提供讀入、寫出的文件
        File file1 = new File(src);
        File file2 = new File(dest);
        try {
            fr= new FileReader(file1);
            fw = new FileWriter(file2);
            // 3.每次讀取的字符數
            char[] c = new char[24];
            int len;
            while ((len = fr.read(c)) != -1) {
                // fos.write(c);//錯誤的寫法兩種: fos.write(c,0,c.length);
                fos.write(c, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
public void testCopyFile(){
    long start = System.currentTimeMillis();
    String src = "C:\\Users\\shkstart\\Desktop\\dbcp.txt";
    String dest = "C:\\Users\\shkstart\\Desktop\\dbcp1.txt";
    copyFile(src,dest);
    long end = System.currentTimeMillis();
    System.out.println("花費的時間為:" + (end - start));//3198
}

對管道進行操作:PipedInputStream(字節輸入流),PipedOutStream(字節輸出流),PipedReader(字符輸入流),PipedWriter(字符輸出流)

PipedInputStream的一個實例要和PipedOutputStream的一個實例共同使用,共同完成管道的讀取寫入操作。主要用於線程操作。

字節/字符數組:ByteArrayInputStreamByteArrayOutputStreamCharArrayReader

CharArrayWriter是在內存中開辟了一個字節或字符數組。

Buffered緩沖流:BufferedInputStreamBufferedOutputStream (flush()刷新緩沖區),BufferedReader (readLine()讀取一行),BufferedWriter (flush()刷新緩沖區) ,是帶緩沖區的處理流,緩沖區的作用的主要目的是:避免每次和硬盤打交道,提高數據訪問的效率。

例子:

//使用緩沖流實現文件的復制的方法
    public void copyFile(String src,String dest){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.提供讀入、寫出的文件
            File file1 = new File(src);
            File file2 = new File(dest);
            //2.想創建相應的節點流:FileInputStream、FileOutputStream
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);
            //3.將創建的節點流的對象作為形參傳遞給緩沖流的構造器中
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //4.具體的實現文件復制的操作
            byte[] b = new byte[1024];
            int len;
            while((len = bis.read(b)) != -1){
                bos.write(b, 0, len);
                bos.flush();
            }
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //5.關閉相應的流
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            
        }
    }

轉化流:InputStreamReader/OutputStreamWriter,把字節轉化成字符。

    /*
     * 如何實現字節流與字符流之間的轉換:
     * 轉換流:InputStreamReader  OutputStreamWriter
     * 編碼:字符串  --->字節數組
     * 解碼:字節數組--->字符串
     */
    @Test
    public void test1(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //解碼
            File file = new File("dbcp.txt");
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            br = new BufferedReader(isr);
            //編碼
            File file1 = new File("dbcp4.txt");
            FileOutputStream fos = new FileOutputStream(file1);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            bw = new BufferedWriter(osw);
            String str;
            while((str = br.readLine()) != null){
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
        }
        
    }


數據流:DataInputStreamDataOutputStream

因為平時若是我們輸出一個8個字節的long類型或4個字節的float類型,那怎麽辦呢?可以一個字節一個字節輸出,也可以把轉換成字符串輸出,但是這樣轉換費時間,若是直接輸出該多好啊,因此這個數據流就解決了我們輸出數據類型的困難。數據流可以直接輸出float類型或long類型,提高了數據讀寫的效率。

打印流:printStreamprintWriter,一般是打印到控制臺,可以進行控制打印的地方。

對象流:ObjectInputStreamObjectOutputStream,把封裝的對象直接輸出,而不是一個個在轉換成字符串再輸出。

序列化流:SequenceInputStream

對象序列化:把對象直接轉換成二進制,寫入介質中。

使用對象流需要實現Serializable接口,否則會報錯。而若用transient關鍵字修飾成員變量,不寫入該成員變量,若是引用類型的成員變量為null,值類型的成員變量為0.


參考:http://blog.csdn.net/yuebinghaoyuan/article/details/7388059/

本文出自 “ciyo技術分享” 博客,請務必保留此出處http://ciyorecord.blog.51cto.com/6010867/1936503

Java IO流