【Java IO流】節點流(或文件流)

節點流(或文件流)

  • 定義文件路徑時,注意:可以用“/”或者“\”。
  • 在寫入一個文件時,如果使用構造器FileOutputStream(file),則目錄下有同名文 件將被覆蓋。
  • 如果使用構造器FileOutputStream(file,true),則目錄下的同名文件不會被覆蓋, 在文件內容末尾追加內容。
  • 在讀取文件時,必須保證該文件已存在,否則報異常。
  • 字節流操作字節,比如:.mp3,.avi,.rmvb,mp4,.jpg,.doc,.ppt
  • 字符流操作字符,只能操作普通文本文件。最常見的文本文 件:.txt,.java,.c,.cpp 等語言的源代碼。尤其注意.doc,excel,ppt這些不是文 本文件

字符流:FileReader & FileWriter

FileReader:

FileReader說明點:

  1. read()的理解:返回讀入的一個字符。如果達到文件末尾,返回-1
  2. 異常的處理:爲了保證流資源一定可以執行關閉操作。需要使用try-catch-finally處理
  3. 讀入的文件一定要存在,否則就會報FileNotFoundException。

Demo:把 hello.txt 裏的內容輸出到控制檯

@Test
    public void test(){
        FileReader fileReader = null;
        try {
            //1.實例化File類的對象,指明要操作的文件
            File file = new File("hello.txt");//相較於當前Module
            //2.創建輸入流對象
            fileReader = new FileReader(file);

            //3.數據的讀入
            //read():返回讀入的一個字符。如果達到文件末尾,返回-1
            int data;
            while ((data = fileReader.read()) != -1 ){
                System.out.print(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
         //4.資源的關閉
            if (fileReader != null)
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

其中,數據的讀入操作可以使用 read() 的重載方法:

            //3.讀入的操作
            //read(char[] cbuf):返回每次讀入cbuf數組中的字符的個數。如果達到文件末尾,返回-1
            char[] cbuf = new char[5];
            int len;
            while ((len = fileReader.read(cbuf)) != -1) {
                for (int i=0;i<len;i++){
                    System.out.print(cbuf[i]);
                }
            }
            //3.讀入的操作
            //read(char[] cbuf):返回每次讀入cbuf數組中的字符的個數。如果達到文件末尾,返回-1
            char[] cbuf = new char[5];
            int len;
            while ((len = fileReader.read(cbuf)) != -1) {
                String str = new String(cbuf,0,len);
                System.out.print(str);
            }

FileWriter:

FileWriter說明:

  • 輸出操作,對應的File可以不存在的。並不會報異常
  • File對應的硬盤中的文件如果不存在,在輸出的過程中,會自動創建此文件。
  • File對應的硬盤中的文件如果存在:
    • 如果流使用的構造器是:FileWriter(file,false) / FileWriter(file):對原有文件的覆蓋
    • 如果流使用的構造器是:FileWriter(file,true):不會對原有文件覆蓋,而是在原有文件基礎上追加內容

Demo:從程序中寫入數據並生成到硬盤的文件裏。

    @Test
    public void testFileWriter(){
        FileWriter fileWriter = null;
        try {
            //1.提供File類的對象,指明寫出到的文件
            File file = new File("hello1.txt");

            //2.提供FileWriter的對象,用於數據的寫出
            fileWriter = new FileWriter(file,true);//append:是否追加寫入

            //3.寫出的操作
            fileWriter.write("I have a dream!\n");
            fileWriter.write("you need to have a dream!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流資源的關閉
            if (fileWriter!=null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

實現文本文件的複製操作

    @Test
    public void testFileReaderFileWriter(){
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            //1.創建File類的對象,指明讀入和寫出的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello2.txt");

            //2.創建輸入流和輸出流的對象
            fileReader = new FileReader(srcFile);
            fileWriter = new FileWriter(destFile,true);

            //3.數據的讀入和寫出的操作
            char[] cbuf = new char[5];
            int len; //記錄每次讀入到cbuf數組中的字符的個數
            while ((len = fileReader.read(cbuf)) != -1){
                //每次寫出len個字符
                fileWriter.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.關閉流資源
            if (fileWriter != null)
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (fileReader != null)
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

字節流:FileInputStream & FileOutputStream

實現圖片的複製操作

    @Test
    public void testFileInputStreamFileOutputStream(){

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            //1.創建File類的對象,指明讀入和寫出的文件
            File srcImg = new File("愛情與友情.jpg");
            File destImg = new File("愛情與友情1.jpg");

            //2.創建輸入流和輸出流的對象
            fileInputStream = new FileInputStream(srcImg);
            fileOutputStream = new FileOutputStream(destImg);

            //3.數據的讀入和寫出的操作
            byte[] cbuf = new byte[8];
            int len;
            while ((len = fileInputStream.read(cbuf)) != -1){
                fileOutputStream.write(cbuf,0,len);
            }

        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.關閉流資源
            if (fileOutputStream!=null)
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (fileInputStream!=null)
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章