Java IO(四)------字節輸入輸出流

 一、字節輸出流:OutputStream數組

 

  這個抽象類是表示字節輸出流的全部類的超類。 輸出流接收輸出字節並將其發送到某個接收器。函數

  方法摘要:spa

  

 

FileOutputStream

OutputStream有不少子類,其中子類FileOutputStream可用來寫入數據到文件。code

FileOutputStream,即文件輸出流是用於將數據寫入 File的輸出流。對象

1.1.1 FileOutputStream類寫入數據到文件中

  

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //需求:將數據寫入到文件中。
        //建立存儲數據的文件。
        File file = new File("c:\\file.txt");
        //建立一個用於操做文件的字節輸出流對象。一建立就必須明確數據存儲目的地。
        //輸出流目的是文件,會自動建立。若是文件存在,則覆蓋。
        FileOutputStream fos = new FileOutputStream(file);
        //調用父類中的write方法。
        byte[] data = "abcde".getBytes();
        fos.write(data);
        //關閉流資源。
        fos.close();
    }
}

 給文件中續寫和換行

咱們直接new FileOutputStream(file)這樣建立對象,寫入數據,會覆蓋原有的文件,那麼咱們想在原有的文件中續寫內容怎麼辦呢?blog

繼續查閱FileOutputStreamAPI。發如今FileOutputStream的構造函數中,能夠接受一個boolean類型的值,若是值true,就會在文件末位繼續添加。資源

構造方法get

 

 

給文件中續寫數據和換行,代碼演示:it

 

public class FileOutputStreamDemo2 {
    public static void main(String[] args) throws Exception {
        File file = new File("c:\\file.txt");
        FileOutputStream fos = new FileOutputStream(file, true);
        String str = "\r\n"+"aaa";
        fos.write(str.getBytes());
        fos.close();
    }
}

異常的處理

 

public class FileOutputStreamDemo3 {
    public static void main(String[] args) {
        File file = new File("c:\\file.txt");
        //定義FileOutputStream的引用
        FileOutputStream fos = null;
        try {
            //建立FileOutputStream對象
            fos = new FileOutputStream(file);
            //寫出數據
            fos.write("abcde".getBytes());
        } catch (IOException e) {
            System.out.println(e.toString() + "----");
throw new RuntimeException("文件寫入失敗,重試");

        } finally {
            //必定要判斷fos是否爲null,只有不爲null時,才能夠關閉資源
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException("關閉資源失敗");
                }
            }
        }
    }
}

 

 

 

 二、字節輸入流:InputStreamio

 這個抽象類是表示輸入字節流的全部類的超類。

  方法摘要:

  

  

 

 

  

 FileInputStream

InputStream有不少子類,其中子類FileInputStream可用來讀取文件內容。

FileInputStream 從文件系統中的某個文件中得到輸入字節。

 

 FileInputStream類讀取數據read方法

在讀取文件中的數據時,調用read方法,實現從文件中讀取數據

 

public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("c:\\file.txt");
        //建立一個字節輸入流對象,必須明確數據源,其實就是建立字節讀取流和數據源相關聯。
        FileInputStream fis = new FileInputStream(file);
        //讀取數據。使用 read();一次讀一個字節。
        int ch = 0;
        while((ch=fis.read())!=-1){
            System.out.println("ch="+(char)ch);
        }
        // 關閉資源。
        fis.close();
    }
}

讀取數據read(byte[])方法

public class FileInputStreamDemo2 {
    public static void main(String[] args) throws IOException {
        /*
         * 演示第二個讀取方法, read(byte[]);
         */
        File file = new File("c:\\file.txt");
        // 建立一個字節輸入流對象,必須明確數據源,其實就是建立字節讀取流和數據源相關聯。
        FileInputStream fis = new FileInputStream(file);        
        //建立一個字節數組。
        byte[] buf = new byte[1024];//長度能夠定義成1024的整數倍。        
        int len = 0;
        while((len=fis.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }
        fis.close();
    }
}

複製文件

 

public class CopyFileTest {
    public static void main(String[] args) throws IOException {
        //1,明確源和目的。
        File srcFile = new File("c:\\YesDir\test.JPG");
        File destFile = new File("copyTest.JPG");
        
        //2,明確字節流 輸入流和源相關聯,輸出流和目的關聯。
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        
        //3, 使用輸入流的讀取方法讀取字節,並將字節寫入到目的中。
        int ch = 0;
        while((ch=fis.read())!=-1){
            fos.write(ch);
        }
        //4,關閉資源。
        fos.close();
        fis.close();
    }
}

 

緩衝數組方式複製文件

public class CopyFileByBufferTest {
    public static void main(String[] args) throws IOException {
        File srcFile = new File("c:\\YesDir\test.JPG");
        File destFile = new File("copyTest.JPG");
        // 明確字節流 輸入流和源相關聯,輸出流和目的關聯。
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        //定義一個緩衝區。
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = fis.read(buf)) != -1) {
            fos.write(buf, 0, len);// 將數組中的指定長度的數據寫入到輸出流中。
        }
        // 關閉資源。
        fos.close();
        fis.close();
    }
相關文章
相關標籤/搜索