輸入輸出資料流——位元組流部分,輸入輸出位元組流

來源:互聯網
上載者:User

輸入輸出資料流——位元組流部分,輸入輸出位元組流

在檔案之後 比較合適出現的 就是 關於 流的操作了。

猜想關於資料的寫入和寫出操作,本質上是01形式的位元,而這些資料的相片順序是不能亂套的。他們是一個有序的整體,這隊長長的用於表示一些內容的東西就稱作流了。在這裡面用Stream 來標識。

 java.io 包中定義了多個流類,用來實現輸入/輸出功能,以不同的角度可以分類為:

   1、按資料流的方向分為輸入和輸出。

   2、按資料處理的單位分為位元組流和字元流。

   3、按功能不同分為節點流和處理流。

package IOPart;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class IOPart {        public static void main(String[] args) {        /**         * 在F:\tryFile目錄下,建立一個a.txt 檔案,修改裡面的內容為:used to try the first demo of inputStream.         */        try {            //如果 在這個 路徑找不到 這樣一個 檔案的話,就會報出,檔案未找到異常,所以 要用 FileNotFoundException 包裹            FileInputStream fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt"));            byte[] contents = new byte[1024];            fileInputStream.read(contents);//這裡會報一個 讀寫異常,再從檔案流往記憶體中的數組中寫入資料的時候,可能會出現問題,需要用IOException包裹一下            String result = new String(contents);            System.out.println(result);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

運行結果:

used to try the first demo of inputStream.

//這個編輯器還真是進階,自動抹掉了我後面茫茫多的空格。事實上因為 我們 建立了一個 長達 1024個 bite位的數組。所以。有多少讀多少,沒有填空,補足。這樣就會有弊端,我們要儲存的資料的內容的大小不確定,那麼這個 byte給多大合適呢?

方案一:

package IOPart;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class IOPart {        public static void main(String[] args) {        /**         * 在F:\tryFile目錄下,建立一個a.txt 檔案,修改裡面的內容為:used to try the first demo of inputStream.         */        try {            //如果 在這個 路徑找不到 這樣一個 檔案的話,就會報出,檔案未找到異常,所以 要用 FileNotFoundException 包裹            FileInputStream fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt"));            byte[] contents = new byte[fileInputStream.available()];            fileInputStream.read(contents);//這裡會報一個 讀寫異常,再從檔案流往記憶體中的數組中寫入資料的時候,可能會出現問題,需要用IOException包裹一下            String result = new String(contents);            System.out.println(result);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

流作為資源的一種使用之後是要關閉的。

package IOPart;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class IOPart {        public static void main(String[] args) {        /**         * 在F:\tryFile目錄下,建立一個a.txt 檔案,修改裡面的內容為:used to try the first demo of inputStream.         */        FileInputStream fileInputStream=null;        try {            //如果 在這個 路徑找不到 這樣一個 檔案的話,就會報出,檔案未找到異常,所以 要用 FileNotFoundException 包裹            fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt"));            byte[] contents = new byte[fileInputStream.available()];            fileInputStream.read(contents);//這裡會報一個 讀寫異常,再從檔案流往記憶體中的數組中寫入資料的時候,可能會出現問題,需要用IOException包裹一下            String result = new String(contents);            System.out.println(result);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            if(fileInputStream!=null){                try {                    fileInputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

這種抽取形式,還是比較重要的。

方式二:

package IOPart;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class IOPart {        public static void main(String[] args) {        IOPart ioPart = new IOPart();        ioPart.method2();            }    private void method2() {        FileInputStream fileInputStream = null;        try {            fileInputStream= new FileInputStream(new File("f:\\tryFile\\SendPart.java"));            byte[] contents = new byte[1024];            int length = 0;            while((length=fileInputStream.read(contents))!=-1){                System.out.println(new String(contents,0,length));            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        finally {            if(fileInputStream!=null){                try {                    fileInputStream.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }    private void method1() {        /**         * 在F:\tryFile目錄下,建立一個a.txt 檔案,修改裡面的內容為:used to try the first demo of inputStream.         */        FileInputStream fileInputStream=null;        try {            //如果 在這個 路徑找不到 這樣一個 檔案的話,就會報出,檔案未找到異常,所以 要用 FileNotFoundException 包裹            fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt"));            byte[] contents = new byte[fileInputStream.available()];            fileInputStream.read(contents);//這裡會報一個 讀寫異常,再從檔案流往記憶體中的數組中寫入資料的時候,可能會出現問題,需要用IOException包裹一下            String result = new String(contents);            System.out.println(result);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            if(fileInputStream!=null){                try {                    fileInputStream.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}

寫出:

package IOPart;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class OutputDemo1 {    public static void main(String[] args) {                FileOutputStream fileOutputStream = null;        try {            fileOutputStream = new FileOutputStream(new File("F:\\tryFile\\output.txt"));            String contentString = new String("lifei");            fileOutputStream.write(contentString.getBytes());            System.out.println("寫出成功");        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            if(fileOutputStream!=null){                try {                    fileOutputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

位元組流拷貝檔案:

package IOPart;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class CopyFile1 {        public static void main(String[] args) {                method2();            }    private static void method2() {        FileInputStream fileInputStream = null;        FileOutputStream fileOutputStream = null;        try {//C:\\Users\\Administrator\\Desktop\\震撼世界的演講《夢想》 標清(270P).qlv            //fileInputStream  = new FileInputStream(new File("f:/tryFile/SendPart.java"));            /**             * 這裡面還可以複製的有 視頻檔案,音頻檔案,其實就是 各種檔案了,只要把要拷貝的檔案,放在FileInputStream裡面就可以了。當然針對字元 還有專門為字元打造的操作流對象。             */            fileInputStream  = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\震撼世界的演講《夢想》 標清(270P).qlv"));            fileOutputStream  = new FileOutputStream(new File("f:/tryFile/copy.qlv"));                        byte[] contents = new byte[1024];            int length = 0;            while((length=fileInputStream.read(contents))!=-1){                fileOutputStream.write(contents, 0, length);            }                    } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            if(fileInputStream!=null){                try {                    fileInputStream.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if(fileOutputStream!=null){                try {                    fileOutputStream.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }        System.out.println("複製完成");    }    private static void method1() {        FileInputStream fileInputStream = null;        FileOutputStream fileOutputStream = null;        try {            fileInputStream  = new FileInputStream(new File("f:/tryFile/SendPart.java"));            fileOutputStream  = new FileOutputStream(new File("f:/tryFile/copy.txt"));            byte[] contents = new byte[fileInputStream.available()];            fileInputStream.read(contents);            fileOutputStream.write(contents);        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            if(fileInputStream!=null){                try {                    fileInputStream.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if(fileOutputStream!=null){                try {                    fileOutputStream.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }        System.out.println("複製完成");    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.