1. 程式人生 > >Java——字節流,字符流

Java——字節流,字符流

close space 保存 ace 宋體 細節 輸出 clas 輸入流

一、字節輸出流

OutputStream抽象類

此抽象類,是表示輸出字節流的所有類的超類。操作的數據都是字節,定義了輸出字節流的基本共性功能方法。

  • 字節: 這樣流每次只操作文件中的1個字節
  • 流對象:操作文件的時候,自己不做依賴操作系統

作用:從Java程序,寫入文件(可以寫任意文件)

方法:

  • write(int b) 寫入1個字節
  • write(byte[] b) 寫入字節數組
  • write(byte[] b,int,int)寫入字節數組,int 開始寫入的索引, int 寫幾個
  • close() 方法,關閉流對象,釋放與此流相關的資源

FileOutputStream類

OutputStream有很多子類,其中子類FileOutputStream可用來寫入數據到文件。

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

流對象使用步驟:

  • 1. 創建流子類的對象,綁定數據目的
  • 2. 調用流對象的方法write寫
  • 3. close釋放資源

1、寫入文件

public static void main(String[] args)throws IOException {
	//流對象的構造方法,可以創建文件,如果文件存在,直接覆蓋
	FileOutputStream fos = new FileOutputStream("c:\\a.txt");
	//流對象的方法write寫數據
	//寫1個字節,查詢編碼表
	fos.write(97);
	
	//寫字節數組
	byte[] bytes = {65,66,67,68};
	fos.write(bytes);
	
	//寫字節數組的一部分,開始索引,寫幾個
	fos.write(bytes, 1, 2);
	
	//寫入字節數組的簡便方式
	//寫字符串
	fos.write("hello".getBytes());

	//關閉資源
	fos.close();
}

2、IO流的異常處理

import java.io.FileOutputStream;
import java.io.IOException;

/*
 *   IO流的異常處理
 *   try catch finally
 *   
 *   細節:
 *     1. 保證流對象變量,作用域足夠
 *     2. catch裏面,怎麽處理異常
 *         輸出異常的信息,目的看到哪裏出現了問題
 *         停下程序,從新嘗試
 *     3. 如果流對象建立失敗了,需要關閉資源嗎
 *         new 對象的時候,失敗了,沒有占用系統資源
 *         釋放資源的時候,對流對象判斷null
 *         變量不是null,對象建立成功,需要關閉資源
 */
public class FileOutputStreamDemo3 {
	public static void main(String[] args) {
		//try 外面聲明變量,try 裏面建立對象
		FileOutputStream fos = null;
		try{
			fos = new FileOutputStream("s:\\a.txt");
			fos.write(100);
		}catch(IOException ex){
			System.out.println(ex);
			throw new RuntimeException("文件寫入失敗,重試");
		}finally{
			try{
				if(fos!=null)
			      fos.close();
			}catch(IOException ex){
				throw new RuntimeException("關閉資源失敗");
			}
		}
	}
}

3、字符串轉成byte數組

"hello,world!".getBytes()

4、文件續寫

FileOutputStream fos = new FileOutputStream(file, true);

二、字節輸入流

InputStream抽象類

通過InputStream可以實現。InputStream此抽象類,是表示字節輸入流的所有類的超類。,定義了字節輸入流的基本共性功能方法。

  • int read():讀取一個字節並返回,沒有字節返回-1.

  • int read(byte[]): 讀取一定量的字節數,並存儲到字節數組中,返回讀取到的字節數,沒有字節返回-1.

FileInputStream

輸入流讀取文件的步驟

  • 1. 創建字節輸入流的子類對象
  • 2. 調用讀取方法read讀取
  • 3. 關閉資源

read()方法,

  • read()執行一次,就會自動讀取下一個字節
  • 返回值,返回的是讀取到的字節, 讀取到結尾返回-1

1、讀取單個字節

public static void main(String[] args) throws IOException{
	FileInputStream fis = new FileInputStream("c:\\a.txt");
	//讀取一個字節,調用方法read 返回int
	//使用循環方式,讀取文件,  循環結束的條件  read()方法返回-1
	int len = 0;//接受read方法的返回值
    
	while( (len = fis.read()) != -1){  // 返回值保存到len中
		System.out.print((char)len);
	}
	//關閉資源
	fis.close();
}

2、讀取一個字節數組

public static void main(String[] args) throws IOException {
	FileInputStream fis = new FileInputStream("c:\\a.txt");
	//創建字節數組
	byte[] b = new byte[1024];
	
	int len = 0 ;
	while( (len = fis.read(b)) !=-1){  // 保存到字節數組b中
		System.out.print(new String(b,0,len));
	}
	fis.close();
}

Copy文件

public static void main(String[] args) {
	long s = System.currentTimeMillis();
	FileInputStream fis = null;
	FileOutputStream fos = null;
	try{
		fis = new FileInputStream("c:\\t.zip");
		fos = new FileOutputStream("d:\\t.zip");
		//定義字節數組,緩沖
		byte[] bytes = new byte[1024*10];
		//讀取數組,寫入數組
		int len = 0 ; 
		while((len = fis.read(bytes))!=-1){
			fos.write(bytes, 0, len);
		}
	}catch(IOException ex){
		System.out.println(ex);
		throw new RuntimeException("文件復制失敗");
	}finally{
		try{
			if(fos!=null)
				fos.close();
		}catch(IOException ex){
			throw new RuntimeException("釋放資源失敗");
		}finally{
			try{
				if(fis!=null)
					fis.close();
			}catch(IOException ex){
				throw new RuntimeException("釋放資源失敗");
			}
		}
	}
	long e = System.currentTimeMillis();
	System.out.println(e-s);
}

三、字符輸入/輸出流

1、字符輸入——FileReader類

字符輸入流只能讀取文本文件,所有字符輸入流的超類為java.io.Reader類

  • int read() 讀取1個字符
  • int read(char[] c) 讀取字符數組
public static void main(String[] args) throws IOException{
	FileReader fr = new FileReader("c:\\1.txt");
	// 讀取單個字符
	/*int len = 0 ;
	while((len = fr.read())!=-1){
		System.out.print((char)len);
	}*/
	//字符數組
	char[] ch = new char[1024];
	int len = 0 ;
	while((len = fr.read(ch))!=-1){
		System.out.print(new String(ch,0,len));
	}
	fr.close();
}

2、字符輸出流——FileWriter

所有字符輸出流的超類為java.io.Writer

  • write(int c) 寫1個字符
  • write(char[] c)寫字符數組
  • write(char[] c,int,int)字符數組一部分,開始索引,寫幾個
  • write(String s) 寫入字符串
public static void main(String[] args) throws IOException{
	FileWriter fw = new FileWriter("c:\\1.txt");
	
	//寫1個字符
	fw.write(100);
	fw.flush();
	
	//寫1個字符數組
	char[] c = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘};
	fw.write(c);
	fw.flush();
	
	//寫字符數組一部分
	fw.write(c, 2, 2);
	fw.flush();
	
	//寫入字符串
	fw.write("hello");
	fw.flush();
	
	fw.close();
}

Copy文件

public static void main(String[] args) {
	FileReader fr = null;
	FileWriter fw = null;
	try{
		fr = new FileReader("c:\\1.txt");
		fw = new FileWriter("d:\\1.txt");
		char[] cbuf = new char[1024];
		int len = 0 ;
		while(( len = fr.read(cbuf))!=-1){
			fw.write(cbuf, 0, len);
			fw.flush();
		}
		
	}catch(IOException ex){
		System.out.println(ex);
		throw new RuntimeException("復制失敗");
	}finally{
		try{
			if(fw!=null)
				fw.close();
		}catch(IOException ex){
			throw new RuntimeException("釋放資源失敗");
		}finally{
			try{
				if(fr!=null)
					fr.close();
			}catch(IOException ex){
				throw new RuntimeException("釋放資源失敗");
			}
		}
	}
}

Java——字節流,字符流