天天看點

黑馬程式員_Java_IO流_位元組流

------Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練、期待與您交流! ------- 一、位元組流概述

1、 位元組流和字元流的基本操作是相同的,但位元組流還可以操作其他媒體檔案。

2、 由于媒體檔案資料中都是以位元組存儲的,是以,位元組流對象可直接對媒體檔案的資料寫入到檔案中,而可以不用再進行刷流動作。

3、 讀寫位元組流:InputStream   輸入流(讀)

                              OutputStream  輸出流(寫)

4、 為何不用進行刷流動作:

        因為位元組流操作的是位元組,即資料的最小機關,不需要像字元流一樣要進行轉換為位元組。是以可直接将位元組資料寫入到指定檔案中。

二、位元組輸入流

2.1 InputStream

1、 InputStream:此抽象類表示位元組輸入流的縮影類的超類。需要定義InputStream子類的應用程式必須總是提供傳回下一個輸入位元組的方法。

2、 直接已知的子類:AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream

3、 方法

int available();//傳回文本内容的總計位元組數。可以擷取文本的大小。傳回此輸入流下一個方法調用可以不受阻塞地從此輸入流讀取(或跳過)的估計位元組數。

void mark(int readlimit);//測試此輸入流是否支援mark和reset方法。

read();//讀取的是單個位元組。

close();//關閉位元組流

void reset();//将此流重新定位到最後一次對此輸入流調用mark方法時的位置。

long skip(long n);//跳過和丢棄此輸入流中的資料的n個位元組。

2.2 FileInputStream

1、 FileInputStream 從檔案系統中的某個檔案中獲得輸入位元組。FileInputStream 用于讀取諸如圖像資料之類的原始位元組流。要讀取字元流,請考慮使用 FileReader。

2、 構造方法

FileInputStream(File file);//通過打開一個到實際檔案的連接配接來建立一個 FileInputStream,該檔案通過檔案系統中的 File 對象 file 指定。

FileInputStream(String name);//通過打開一個到實際檔案的連接配接來建立一個 FileInputStream,該檔案通過檔案系統中的路徑名 name 指定

3、 read();//從此輸入流中讀取一個資料位元組。如果沒有輸入可用,則此方法将阻塞。傳回下一個資料位元組;如果已到達檔案末尾,則傳回 -1。

程式示例:

public class FileStream {

	public static void main(String[] args) throws IOException {
		ReadFile_1();
	}
	
	public static void ReadFile_1() throws IOException{
		
		FileInputStream fis=new FileInputStream("fos.txt");
		
		int ch=0;
		while((ch=fis.read())!=-1){
			System.out.print((char)ch);
		}
		fis.close();
	}
}
           

2.3 BufferedInputStream

1、 在建立 BufferedInputStream 時,會建立一個内部緩沖區數組。在讀取或跳過流中的位元組時,可根據需要從包含的輸入流再次填充該内部緩沖區,一次填充多個位元組。

2、 構造方法

BufferedInputStream(InputStream in);//建立一個 BufferedInputStream 并儲存其參數,即輸入流 in,以便将來使用。

BufferedInputStream(InputStream in, int size);// 建立具有指定緩沖區大小的 BufferedInputStream 并儲存其參數,即輸入流 in,以便将來使用。

3、 自己構造一個BufferedInputStream緩沖區

程式示例:

package demo.iostream;

import java.io.*;

public class MyBufferedInputStreamDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

class MyBufferedInputStream{
	private InputStream in;
	private byte[] buf=new byte[1024];
	private int pos=0,count=0;
	MyBufferedInputStream(InputStream in){
		this.in=in;
	}
	
	public int myRead() throws IOException{
		
		if(count==0){
			count=in.read(buf);
			if(count<0)
				return -1;
			pos=0;
			byte b=buf[pos];
			count--;
			pos++;
			return b & 255;
		}
		else if(count>0){
			
			byte b=buf[pos];
			count--;
			pos++;
			return b & 255;
		}
		return -1;
		
		
	}
	
	public void myClose() throws IOException{
		in.close();
	}
}
           

2.4 DataInputStream

1、 用于操作基本資料類型的位元組輸入流,資料輸入流允許應用程式以與機器無關方式從底層輸入流中讀取基本 Java 資料類型。應用程式可以使用資料輸出流寫入稍後由資料輸入流讀取的資料。

2、 構造方法

DataInputStream(InputStream in) ;//使用指定的底層 InputStream 建立一個 DataInputStream。

3、 方法

讀取基本資料類型的方法:readBoolean()、readByte()、readChar()、readDouble()、readFloat()、readInt()、readLong()、readShort()。

readUTF(),讀入一個已使用 UTF-8 修改版格式編碼的字元串。

程式示例:

public static void readData() throws IOException{
		DataInputStream dis=new DataInputStream(new FileInputStream("data.txt"));
		int num=dis.readInt();
		boolean b=dis.readBoolean();
		double d=dis.readDouble();
		System.out.println(num);
		System.out.println(b);
		System.out.println(d);
		dis.close();
		
	}
           

三、位元組輸出流

3.1 OutputStream

1、 OutputStream:此抽象類是表示輸出位元組流的所有類的超類。輸出流接受輸出位元組,并将這些位元組發送到某個接收器。需要定義OutputStream子類的應用程式必須始終提供至少一個可寫入一個輸出位元組的方法。

2、 已知直接子類:ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, OutputStream, PipedOutputStream

3、 方法

void close();//關閉此輸出流并釋放與此流有關的所有系統資源

void flush();//重新整理此輸出流并強制寫出所有緩沖的輸出位元組

void write(byte[] b);//将b.length個位元組從指定的byte數組寫入此輸出流

void write(byte[] b,int off,int len);//将指定byte數組中從偏移量off開始的len個位元組寫入此輸出流。

3.2 FIleOutputStream

1、 檔案輸出流是用于将資料寫入 File 或 FileDescriptor 的輸出流。FileOutputStream 用于寫入諸如圖像資料之類的原始位元組的流。

2、 構造方法

FileOutputStream(File file);//建立一個向指定 File 對象表示的檔案中寫入資料的檔案輸出流。

  FileOutputStream(File file, boolean append);// 建立一個向指定 File 對象表示的檔案中寫入資料的檔案輸出流。若append為true,則在檔案後續寫,否則覆寫。

FileOutputStream(String name);//建立一個向具有指定名稱的檔案中寫入資料的輸出檔案流。

  FileOutputStream(String name, boolean append);//建立一個向具有指定 name 的檔案中寫入資料的輸出檔案流。若append為true,則在檔案後續寫,否則覆寫。

3、 複制圖檔示例:

package demo.iostream;

/*
複制一個圖檔
思路:
1,用位元組讀取流對象和圖檔關聯
,用位元組寫入流對象建立一個圖檔檔案,用于存儲擷取到的圖檔資料
3,通關循環讀寫,完成資料的存儲
3,關閉資源
 */

import java.io.*;

public class CopyPic {

	public static void main(String[] args) {
		FileOutputStream fos=null;
		FileInputStream fis=null;
		try {
			fos=new FileOutputStream("2.jpg");
			fis=new FileInputStream("1.jpg");
			byte[] buf=new byte[1024];
			int len=0;
			while((len=fis.read(buf))!=-1){
				fos.write(buf,0,len);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(fis!=null){
					fis.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(fos!=null){
					fos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
           

3.3 BufferedOutputStream

1、 該類實作緩沖的輸出流。通過設定這種輸出流,應用程式就可以将各個位元組寫入底層輸出流中,而不必針對每次位元組寫入調用底層系統。

2、 構造方法

BufferedOutputStream(OutputStream out) ;//建立一個新的緩沖輸出流,以将資料寫入指定的底層輸出流。

        BufferedOutputStream(OutputStream out, int size);//建立一個新的緩沖輸出流,以将具有指定緩沖區大小的資料寫入指定的底層輸出流。

3、 通過緩沖區複制mp3檔案(用到之前做的緩沖區)

package demo.iostream;

import java.io.*;

/*
示範mp3的複制,通過緩沖區
BufferedOutputStream
BufferedInputStream
 */
public class CopyMp3 {

	public static void main(String[] args) throws IOException {
		long start=System.currentTimeMillis();
//		Copy_1();
		Copy_2();
		long end=System.currentTimeMillis();
		System.out.println((end-start)+"毫秒");
	}
	
	public static void Copy_2() throws IOException{
		MyBufferedInputStream bufis=new MyBufferedInputStream(new FileInputStream("1.mp3"));
		BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("2.mp3"));
		
		int len=0;
		while((len=bufis.myRead())!=-1){
			bufos.write(len);
		}
		bufis.myClose();
		bufos.close();
	}
	public static void Copy_1() throws IOException{
		BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("1.mp3"));
		BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("2.mp3"));
		
		int len=0;
		while((len=bufis.read())!=-1){
			bufos.write(len);
		}
		bufis.close();
		bufos.close();
	}
}
           

3.4 DataOutputStream

1、 用于操作基本資料類型的位元組輸出流,資料輸出流允許應用程式以适當方式将基本 Java 資料類型寫入輸出流中。然後,應用程式可以使用資料輸入流将資料讀入。 

2、 構造方法

DataOutputStream(OutputStream out);//建立一個新的資料輸出流,将資料寫入指定基礎輸出流。

3、 方法

讀取基本資料類型的方法:writeBoolean(boolean v)、writeByte(int v)、writeChar(int v) 、writeDouble(double v)、writeFloat(float v)、writeInt(int v)、writeLong(long v)、writeShort(int v)。

writeUTF(String str) ,以與機器無關方式使用UTF-8 修改版編碼将一個字元串寫入基礎輸出流。

程式示例:

public static void writeData() throws IOException{
		
		DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt"));
		
		dos.writeInt(234);
		dos.writeBoolean(true);
		dos.writeDouble(426.415);
		
		dos.close();		
	}
           

繼續閱讀