Java IO流

Java IO流

1.File Class

File類代表一個文件或者文件目錄
構造器:

  • File(String pathname)
  • File(String pathname,String child)
  • File(String parent,String child)
    parent目錄路徑 child表示從parent的下一層目錄
        File file = new File("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\123.txt");
        File file1=new File("123.txt");
        File file2=new File("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream","123.txt");
        File file3=new File("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd","iostream");

1.1 File類的獲取方法

  • getAbsolutePath()獲取絕對路徑
  • getAbsoluteFile()獲取絕對路徑表示的文件
  • getPath()獲取路徑
  • getParent()獲取上一層文件目錄路徑,若無返回null
  • length()獲取文件長度(位元組數)
  • lastModified() 獲取最後一次修改時間
  • String [] list()獲取目錄下所有文件和文件夾的名稱數組,返回String數組
  • File[] listFiles()獲取指定目錄下的所有文件夾和文件目錄File數組
        //獲取絕對路徑
        System.out.println(file.getAbsolutePath());
        //獲取絕對路徑表示的文件
        System.out.println(file.getAbsoluteFile());
        //獲取路徑
        System.out.println(file.getPath());
        //獲取上一層文件目錄路徑,若無返回null
        System.out.println(file.getParent());
        //獲取文件長度(位元組數),不能獲取目錄的大小
        System.out.println(file.length());
        //獲取最後一次修改時間
        System.out.println(file.lastModified());
        //獲取目錄下所有文件和文件夾的名稱數組,返回String數組
        String []filenames=file3.list();
        for (String o:filenames)
        {
            System.out.println(o);
        }
        //獲取指定目錄下的所有文件夾和文件目錄File數組
        File [] filenames2=file3.listFiles();
        for (File files : filenames2)
        {
            System.out.println(files.getName());
        }

1.2 File類的重命名與創建文件功能

renameTo()把文件重命名為指定的文件路徑

file2.renameTo(new File("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\suanfa\\binarytree\\123.txt"));

createNewFile() 創建文件,若文件存在,則不創建,返回false
mkdir() 創建文件目錄,如果此文件存在,就不創建,如果上級目錄不存在也不創建
mkdirs() 創建文件目錄,上級目錄不存在也會創建

        //創建文件,若文件存在,則不創建,返回false
        file1.createNewFile();
        //創建文件目錄,如果此文件存在,就不創建,如果上級目錄不存在也不創建
        file4.mkdir();
        //創建文件目錄,上級目錄不存在也會創建
        file4.mkdirs();

2.Java IO流

2.1 Java IO流的概念

Java程式中輸入與輸出都是以流的方式的進行,I/O技術是用於處理設備之間的數據傳輸。在java中把不同的輸入/輸出源(鍵盤,文件,網路連接等)抽象表述為「流」(stream)。通過流的形式允許java程式使用相同的方式來訪問不同的輸入/輸出源。stram是從起源(source)到接收的(sink)的有序數據。
主要的IO流

數據單位 位元組流 字元流
抽象基類 InputStream OutputStream Reader Writer
節點流 FileInputStream FileOutputStream FileReader FileWriter
處理流 BufferedInputStream BufferedOutputStream(數據流、對象流、列印流等) BufferedReader BufferedWriter

2.2 節點流

Java重要的節點流有FileInputStream FileOutputStream(位元組流)和FileReader FileWriter(字元流)
注意:字元流是對位元組流進行封裝,字元流的底層也是位元組流,進行封裝轉化,更方便簡單處理字元文檔(可以用記事本打開的)
例子:使用FileInputStream FileOutputStream(位元組流)進行文件複製

  • 第一步:創建流 輸入流輸出流分別指向
  • 第二步:操作流 創建一個中轉站(位元組數組),在使用輸入流read方法將位元組流讀入數組,直到文件複製完成。
  • 第三步:關閉流
    需要使用try–catch–finally捕獲異常
/***
 * 使用FileInputStream和FileOutputStream實現文件複製
 */
public class TestFileStream {
    public static void main(String[] args) {
        //1.創建流
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try{
            fis = new FileInputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\123.txt");
            fos = new FileOutputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\12345.txt");
            //2.使用流
            //2.1使用一個中轉站
            byte [] buf=new byte[1024];
            int len;//記錄每次讀取位元組長度
            while((len=fis.read(buf))>=0)
            {
                fos.write(buf,0,len);//將讀取的位元組,寫入新的文件中

            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }finally {
            //3.關閉流
            if(fis!=null)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null)
            {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}

例子:使用FileReader FileWriter(字元流)進行txt文檔的複製
注意:字元流只能複製字元文檔,如果要複製影片、音頻、圖片等需要使用位元組流,字元流複製跟位元組流流程一樣,就是中轉站採用的是字元數組。

  • 第一步:創建流 輸入流輸出流分別指向
  • 第二步:操作流 創建一個中轉站(字元數組),在使用輸入流read方法將字元流讀入數組,直到文件複製完成。
  • 第三步:關閉流
    需要使用try–catch–finally捕獲異常
/***
 * 使用FileReader和FileWriter實現文件複製
 */
public class TestFileReader {
    public static void main(String[] args) {
        FileReader fr=null;
        FileWriter fw=null;

        try {
            //創建流
            fr = new FileReader("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\123.txt");
            fw = new FileWriter("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\12345.txt");
            fw = new FileWriter("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\12345.txt",true);//每次在後面追加,不覆蓋
            //使用流
            char []buf=new char[1024];
            int n;
            while((n=fr.read(buf))>=0)
            {
                fw.write(buf,0,n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //關閉流
            try {
                if(fr!=null)
                {
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fw!=null)
                {
                    fw.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

2.2 緩衝流

作用:緩衝流分為快取位元組流和快取字元流,目的是提高訪問速度,提高便捷。
原理:分別開闢一個輸入流緩衝區,和輸出流緩衝區,不用每次讀取寫入都直接操作硬碟,中轉站會先從緩衝區讀取或寫入數據,如果緩衝區沒有或者快取寫滿,這時緩衝區才會操作硬碟,大大減少操作硬碟次數,節約時間。
bb411cfaaa2daf2d330bcd6a894a8279.png
注意

  • 緩衝流BufferedInputStream BufferedOutputStream 和 BufferedReader BufferedWriter是高層流,資源回收時,只需要關閉高層流,低層流會自動的關閉。
  • 何時緩衝區寫入硬碟,三種情況:緩衝區滿、關閉緩衝流、手動使用flush函數。

例子1:使用FileInputStream FileOutputStream(位元組流)進行文件複製,並使用BufferedInputStream BufferedOutputStream緩衝流提高速度。

/**
 * 使用緩衝流Buffered來加快文件複製的過程
 */
public class TestBufferedStream {
    public static void main(String[] args) {
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
            bis=new BufferedInputStream(new FileInputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\2007.07481.pdf"));
            bos=new BufferedOutputStream(new FileOutputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\2007.pdf"));
            byte [] buf=new byte[1024];
            int n;
            while((n=bis.read(buf))!=-1)
            {
                bos.write(buf,0,n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {//關閉流
            if(bis!=null)
            {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos!=null)
            {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

例子2:使用 BufferedReader BufferedWriter(字元流)進行字元文檔複製,要求一行一行的複製

/**
 * 使用緩衝流字元流逐行讀取文檔,並複製到另一個文檔上
 */
public class TestBufferedReader {
    public static void main(String[] args) {
        BufferedReader bor=null;
        BufferedWriter bow=null;
        try {
            //創建流
            bor=new BufferedReader(new FileReader("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\123.txt"));
            bow=new BufferedWriter(new FileWriter("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\12345.txt"));
            //使用流
            String s;
            while((s=bor.readLine())!=null)//如果讀不到數據,返回null
            {
                bow.write(s);//寫入數據
                bow.newLine();//換行
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //關閉流
            try {
                if(bor!=null)
                {
                    bor.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bow!=null)
                {
                    bow.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }

2.3 其他流

數據流 DataInputStream和DataOutputStream

方便操作各種數據類型,可以將各種數據類型,寫入文件,讀取出來也能還原出各種數據類型

public class TestDataInputStream {
    public static void main(String[] args) {
        DataInputStream dain=null;
        DataOutputStream daou=null;
        try{
            //創建數據流
            dain=new DataInputStream(new FileInputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\Data.txt"));
            daou=new DataOutputStream(new FileOutputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\Data.txt"));
            daou.writeBoolean(true);
            daou.writeChar('偉');
            daou.writeInt(12);
            daou.writeUTF("hhhhhhh");
            //讀取數據
            System.out.println(dain.readBoolean());
            System.out.println(dain.readChar());
            System.out.println(dain.readInt());
            System.out.println(dain.readUTF());

        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            if(dain!=null)
            {
                try {
                    dain.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(daou!=null)
            {
                try {
                    daou.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

對象流 ObjectInputStream和ObjectOutputStream

注意
使用對象流寫入和讀取對象時,需要將類繼承Serialization(序列化)或者DeSerialization(反序列化)
對象(記憶體)—->位元組數組(外存,網路)—->(對象)記憶體

  • static 修飾不參加序列化,可以使用transient使類的變數屬性不參加序列化
  • 序列化後不能修改類的內容(除非在創建類的時候,生成類的序列號)
  • 如果對象寫入文件,不僅要保證對象序列化,也要保證對象成員變數序列化

public class TestObjectInputStream {
    public static void main(String[] args) {
        ObjectInputStream dain=null;
        ObjectOutputStream daou=null;
        try{
            //創建數據流
            daou=new ObjectOutputStream(new FileOutputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\Data.txt"));
            dain=new ObjectInputStream(new FileInputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\Data.txt"));
            daou.writeBoolean(true);
            daou.writeChar('偉');
            daou.writeInt(12);
            daou.writeUTF("hhhhhhh");
            daou.writeObject(new Person(0,"cwstd",22));

            //讀取數據
            System.out.println(dain.readBoolean());
            System.out.println(dain.readChar());
            System.out.println(dain.readInt());
            System.out.println(dain.readUTF());
            System.out.println(dain.readObject().toString());

        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            if(dain!=null)
            {
                try {
                    dain.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(daou!=null)
            {
                try {
                    daou.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}