1. 程式人生 > >【Java基礎知識】IO流 詳解

【Java基礎知識】IO流 詳解

1.概念

(1)io流用來處理裝置之間的資料傳輸;
(2)Java對資料的操作的操作是通過流的方式;
(3)Java用於操作流的物件都在IO包;
(4)io流按操作資料分為兩種:位元組流和字元流;
(5)io流按流向分為:輸入流、輸出流

Java流類圖結構:

這裡寫圖片描述



2.位元組流

(1)定義:
用於讀取二進位制檔案及任何型別檔案

(2)InputStream型別

InputStream流圖結構:
這裡寫圖片描述



*ByteArrayInputStream 允許將記憶體中的緩衝區當作InputStream使用
*StringBufferInputStream 將String轉換成InputStream
*FileInputStream 用於從檔案中讀取資訊
*PipedInputStream 產生用於寫入相關PipedOutputStream的資料,實現管道化概念
*SequenceInputStream 將兩個或多個InputStream物件轉換成單一InputStream
*FilterInputStream 抽象類作為裝飾器的介面


(2)OutputStream型別

OutputStream流圖結構:
這裡寫圖片描述



*ByteArrayOutputStream 在記憶體中建立緩衝區,所有送往流的資料都要放置在緩衝區
*FileOutputStream 用於將資訊寫入檔案
*PipedOutputStream 任何寫入其中的資訊都會自動作為相關PipedInputStream的輸出
*FilterOutputStream 抽象類作為裝飾器的介面


3.字元流

(1)定義:
用於讀寫檔案文字,不能操作二進位制檔案。

字元流圖結構:
這裡寫圖片描述

(2)Reader型別

(3)Writer型別



4.檔案輸入與輸出

(1)位元組的輸入與輸出

*從鍵盤輸入

例:

InputStreamReader in = new InputStreamReader(System.in);

*從檔案中讀取

例:

InputStreamReader in = new InputStreamReader(new FileInputStream("dream.txt"));

(2)字元的輸入與輸出

*建立一個檔案物件

例:

FileWriter f = new FileWriter("F:\\BAT\\writefile.txt");

*讀取檔案物件

例:

FileReader fr = new FileReader("dream.txt"
);

*輸出文字格式

例:

PrintWriter pw = new PrintWriter("possible.txt");

*在緩衝區輸入檔案

例:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

*格式化的記憶體輸入

例:

DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("red.txt")));



5.複製檔案

(1)字元流方式

例:

public class CopyFile {

    public static void main(String[] args) {
        //檔案讀(輸入流)字元物件
        FileReader f = null;
        //檔案寫(輸出流)字元物件
        FileWriter f1 = null;

        try {
            f = new FileReader("F:\\學習\\資料\\file1.txt");
            f1 = new FileWriter("F:\\學習\\資料\\writefile1.txt");
            //n記錄實際讀取到的字元數
            int n = 0;
            //讀入到記憶體
            char[] c = new char[1024];
            try {
                while ((n = f.read(c)) != -1){
                    f1.write(c,0,n);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                f.close();
                f1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

(2)位元組流方式

例:

public class CopyPicture {

    public static void main(String[] args) {
        //先把圖片讀入記憶體,再寫入到某個檔案
        //因為是二進位制檔案,因此只能用位元組流完成

        //輸入流
        FileInputStream f = null;
        //輸出流
        FileOutputStream f1 = null;
        try {
            f = new FileInputStream("F:\\學習\\資料\\image\\background_lawn.jpg");
            f1 = new FileOutputStream("F:\\學習\\資料\\copybackground.jpg");
            byte[] bytes = new byte[1024];
            //n記錄實際讀取到的位元組數
            int len = 0;
            //迴圈讀1
                while ((len=f.read(bytes)) != -1){
                    f1.write(bytes,0,len);
                }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                f.close();
                f1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}



6.物件流與序列化

(1)物件流
*操作物件:ObjectInputStream與ObjectOutputStream
被操作的物件需要實現serializable介面(沒有方法的介面,即標記介面)

(2)序列化:
每個物件都用一個序列號儲存

(3)意義:
序列化用序列號代替了記憶體地址。

(4)檔案格式:
物件序列化以特殊的檔案格式儲存物件資料

(5)修改預設的序列化機制
*某些資料域不可被序列化
*將它們標記成是transient(瞬時),因為瞬時的域可以防止序列化。





本人才疏學淺,如有錯誤,請指出~
謝謝!