1. 程式人生 > 實用技巧 >22_IO流之位元組流

22_IO流之位元組流

1. IO流概述

IO流, 即程式進行輸入輸出操作. 狹義的可以理解為對File物件內容的讀和寫. 用來讀取File物件內容的程式為輸入流(inputStream), 往File類中寫內容的程式為輸出流(OutputStream). java中所有輸入,輸出的操作都放置於java.io包中, 所以再使用時需要導包.

IO流廣泛地應用於檔案傳輸和網路程式設計.

2. IO流分類

2.1 按資料的流向

  1. 輸入流
  2. 輸出流

2.2 按資料型別

  1. 位元組流(所有檔案都可操作)
  2. 字元流(一般用於文字檔案)

3. 位元組輸入流(InputStream)

3.1 概述

public abstract class InputStream implements Closeable {}

這個抽象類是表示輸入位元組流的所有類的超類, 具體使用其子類FileInputStream

3.2 FileInputStream類成員

3.2.1 構造器

 public FileInputStream(String name);	//根據字串(檔案路徑名)建立流
     
 public FileInputStream(File file);	//根據檔案建立流


3.2.2 成員方法

public int read(); //讀取一個位元組
public int read(byte b[]);	//讀取一個位元組陣列
public int read(byte b[], int off, int len); //讀取一個位元組的指定長度
public void close();	//關閉流

3.3 Demo

package f_io.b_inpustream;

import java.io.*;

/**
 * 位元組輸入流
 */
public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException {
        //建立物件
        InputStream is = new FileInputStream("a.txt");

        //public int read(); //讀取一個位元組
        //int c = is.read();
        //System.out.println(c);//104

        //迴圈改進
        /*int c;
        while((c=is.read()) != -1) {
            System.out.print((char)c);  //helloworldjava
        }*/


        // public int read(byte b[]);	//讀取一個位元組陣列
        /*byte[] bytes = new byte[5];
        int len;
        while((len=is.read()) != -1) {
            System.out.print((char) len);
        }*/

        //public int read(byte b[], int off, int len); //讀取一個位元組的指定長度
        byte[] bytes = new byte[1024];
        int len;
        while((len=is.read(bytes)) != -1) {
            System.out.print(new String(bytes, 0, len));    //helloworldjava
        }

        //public void close();	//關閉流
        is.close();

    }
}

4. 位元組輸出流OutputStream

4.1 概述

public abstract class OutputStream implements Closeable, Flushable {}

這個抽象類是表示位元組輸出流的所有類的超類, 使用具體類FileOutputStream.

4.2 FileOutputStream類成員

4.2.1 構造器

public FileOutputStream(String name);	//根據檔案路徑建立流
 public FileOutputStream(String name, boolean append); //是否追加寫入
public FileOutputStream(File file); //根據File物件建立流
 public FileOutputStream(File file, boolean append); //是否追加寫入

4.2.2 成員方法

public void write(int b); 寫一個位元組
public void write(byte b[]); 寫一個位元組陣列
public void write(byte b[], int off, int len);寫一個數組指定長度
public void close(); 關閉流

4.3 Demo

package f_io.c_outputstream;

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

/**
 * 位元組輸出流
 */
public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //建立流
        OutputStream os = new FileOutputStream("a.txt");

        //public void write(int b); 寫一個位元組
       /* os.write('h');
        os.write('e');
        os.write('l');
        os.write('l');
        os.write('o'); */ //hello

        //public void write(byte b[]); 寫一個位元組陣列
        /*byte[] bys = {'w','o','r','l','d'};
        os.write(bys);*/  //world

        //public void write(byte b[], int off, int len);寫一個數組指定長度
        byte[] bys = "helloworldjava".getBytes();
        os.write(bys, 0, bys.length);   //helloworldjava

        //public void close(); 關閉流
        os.close();
    }
}

5. 總結

位元組輸入流和輸出流是最基本的IO流, 實際應用的流都是基於此流的封裝.用法基本類似, 在實際應用中參考此用法即可.