天天看點

JAVA中inputStream/OutputStream位元組輸入流、輸出流讀寫檔案

好記性不如賴筆頭……

注意:InputStream/OutputStream是抽象類,不能被執行個體化,隻能執行個體化其子類,且是位元組流,不是字元流

InputStream is = new FileInputStream(“a.txt”); 等同于 InputStream is = new FileInputStream(new File(“a.txt”));

OutputStream類似與之類似,

package com.Ckinghan.outputStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class OutputStreamDemo {

    public static void main(String[] args) {

        /**
         * 使用位元組流寫入檔案
         */
        outputStreadRead();

        /**
         *位元組流的資料讀取方式一:每次讀取一個位元組
         */
        inputStreamReader();

        /**
         * 位元組流的資料讀取方式二:每次讀取一定長度的位元組,建議使用
         */
        inputStreamReader1();


        /**
         * 位元組流的資料讀取方式三:每次讀取一定長度的位元組從指定的數組索引上儲存資料
         */
        inputStreamReader2();
    }


    /**
     * @描述:位元組流的資料讀取方式三:每次讀取一定長度的位元組從指定的數組索引上儲存資料
     * @建立時間:
     */
    public static void inputStreamReader2(){
        //建立位元組對象
        InputStream inputStream = null;
        try {
            //執行個體化對象
            inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");

            /**
             * 每次讀取一定長度的位元組從指定的數組索引上儲存資料,可以讀取中文(檔案中的資料為:OutputStreamp測試寫入資料)
             */
            //定義每次讀取位元組的大小與儲存位元組的資料
            byte[] bs = new byte[];
            //定義每次讀取的長度
            int len = -;
            /**
             * inputStream.read(bs,1,10); 參數說明:
             * bs:數組,每次最大可以儲存1024個位元組
             * 1    :指定的索引位置,這裡是從bs數組的索引1個開始儲存資料
             * 10:從inputStream讀取的位元組個數,從0開始,注意,如果讀取的資料中文,而讀取的位元組個數為奇數,可能會現無法識别或錯誤的資料
             */
            len = inputStream.read(bs,,);
            //輸出字元,注意:在輸出時,應從指定的索引位置進行讀取
            System.out.println(new String(bs,,len)); 

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            //關閉流
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    /**
     * @描述:位元組流的資料讀取方式二:每次讀取一定長度的位元組,建議使用
     * @建立時間:
     */
    public static void inputStreamReader1(){
        //建立位元組對象
        InputStream inputStream = null;
        try {
            //執行個體化對象
            inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");

            /**
             * 讀取一定長度的位元組資料,建議使用,可以讀取中文(檔案中的資料為:OutputStreamp測試寫入資料)
             * 讀取結果為:OutputStreamp測試寫入資料
             */
            //定義每次讀取位元組的大小與儲存位元組的資料
            byte[] bs = new byte[];
            //定義每次讀取的長度
            int len = -;
            //循環讀取資料,如果讀取的資料為-1,說明已經讀取了末尾
            while((len = inputStream.read(bs)) != -){
                //輸出字元
                System.out.println(new String(bs,,len)); 
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            //關閉流
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    /**
     * @描述:位元組流的資料讀取方式一:每次讀取一個位元組
     * @建立時間:
     */
    public static void inputStreamReader(){
        //建立對象
        InputStream inputStream = null;
        try {
            //執行個體化對象
            inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");
            /**
             * 每次讀取一個位元組,對于英文是可以的(檔案中的資料為:OutputStreamp測試寫入資料),
             * 但不能讀取中文,因為編碼我使用的是UTF-8,一個中文占兩個位元組的長度
             * 讀取的結果如下:OutputStreamp????????????
             */
            //建立讀取的字元,儲存的對應的acsii碼
            int b = -;
            //循環讀取字元,如果字元為-1,說明已讀取到檔案的末尾
            while ((b = inputStream.read()) != -) {
                //将讀取的acsii碼轉換為字元并輸出
                System.out.print((char)b);
            }
            System.out.println();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            //關閉流
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * @描述:使用位元組流寫入資料到檔案中
     * @建立時間:
     */
    public static void outputStreadRead(){
        //建立位元組流對象
        OutputStream stream = null;
        try {
            //執行個體化對象
            stream = new FileOutputStream("JavaIOOutputStreamReader.java");
            //要寫入的字元串資料
            String string = "OutputStreamp測試寫入資料";
            //将字元串資料轉換為位元組數組
            byte[] bytes = string.getBytes();
            //将位元組數組寫入到檔案
            stream.write(bytes);
            //清空緩沖區,将寫入的資料儲存
            stream.flush();
            //寫入成功後的提示語
            System.out.println("寫入檔案成功");

            //抛出異常
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //如果stream被執行個體化
            if(stream != null){
                try {
                    //關閉位元組流
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}