1. 程式人生 > >9.27 IO流學習總結(二)

9.27 IO流學習總結(二)

在學習IO流之前我們今天先學習了遞迴。

遞迴

遞迴:就是在方法定義中呼叫方法本身的現象。 在使用遞迴時,必須有一個明確的遞迴結束條件,稱為遞迴出口。

下面舉個例子:

1.用遞迴來實現斐波那契額數列,如1,1,2,3,5,8,13…,輸出前20項的和。

public static int fib(int n){//n表示項數
	if(n==1||n==2){
		return 1;
	}else{
		return fib(n-1)+fib(n-2);
	}

}

public static void main(String[] args) {
	System.out.println(fib(20));	//結果為6765。
}

2.練習:用遞迴刪除帶內容(檔案)的目錄

public static void main(String[] args) {

	File f = new File("F:\\File練習題");
	method(f);
	
}

public static void method(File file) {
	File[] listFiles = file.listFiles();
	for (File file1 : listFiles) {
		if(file1!=null) {  //先判斷檔案物件是否為空,避免出現空指標異常
			if(file1.isDirectory()) {	//然後再判斷該檔案物件是否是目錄,如果是目錄則呼叫本身函式(遞迴),逐個找到檔案並刪除
				method(file1);
			}else {
				file1.delete();
			}
		
		}
	}
}

I/0流

1.I/O流的分類: 流向: 輸入流 讀取資料 輸出流 寫入資料 資料型別: 位元組流: 位元組輸入流 讀取資料 InputStream 位元組輸出流 寫入資料 OutputStream 字元流: 字元輸入流 讀取資料 Reader 字元輸出流 寫入資料 Writer 注意:一般我們在探討IO流的時候,如果沒有明確說明按哪種分類來說,預設情況按照資料型別來分。

今天我們主要學習了位元組流。

位元組流

  1. FileOutputStream(輸出流)的構造器:

FileOutputStream(File file) 建立檔案輸出流以寫入由指定的 File物件表示的檔案。

FileOutputStream(String name) 建立檔案輸出流以指定的名稱寫入檔案。

FileOutputStream(File file, boolean append) 建立檔案輸出流以寫入由指定的 File物件表示的檔案。 預設是false 也就是說預設是不追加在文字的末尾的。

File f = new File("F:\\File練習題");

FileOutputStream fos = null;
try {
	fos = new FileOutputStream(f);//建立檔案輸出流寫入指定檔案物件所表示的檔案中,如果檔案不存在則自動建立
	fos.write("hello world".getBytes());
	fos.write(" 你好!".getBytes());
} catch (FileNotFoundException e) {
	//檔案可能找不到的情況
	e.printStackTrace();
} catch (IOException e) {
	//輸入輸出異常
	e.printStackTrace();
}finally {
	try {
		if(fos!=null){
			fos.close();
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}

2.用位元組流來實現寫入操作。 public void write(int b);寫一個位元組 public void write(byte[] b);寫一個位元組陣列 public void write(byte[] b,int off,int len);寫一個位元組陣列的一部分

例如:

File f = new File("F:\\File練習題");
FileOutputStream fos = null;
try {
	fos = new FileOutputStream(f);
	byte[] bs = {97,88,41,36,77,127};
	 os.write(bs, 0,bs.length);//len是位元組的長度   off位元組陣列的起始下標
	 os.write(bs);
} catch (FileNotFoundException e) {
	//檔案可能找不到的情況
	e.printStackTrace();
} catch (IOException e) {
	//輸入輸出異常
	e.printStackTrace();
}finally {
	try {
		if(fos!=null){
			fos.close();
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}

FileInputStream(位元組輸入流/讀取資料)的操作步驟: A.建立位元組輸入流物件 B.呼叫read()方法讀取操作,並把資料顯示在控制檯中 C.釋放資源

讀取方式: A.int read():一次讀取一個位元組 B.int read(byte[] b):一次讀取一個位元組陣列 C.int read(byte[] b,int off,int len):一次讀取一個部分位元組陣列

例如: 1.

//每次只讀取一個位元組
FileInputStream fis = null;
	try {
	 fis = new FileInputStream("a.txt");
	 int by = 0;
	 while((by = fis.read())!=-1){  //迴圈讀取,讀取完畢則返回-1;
		 System.out.println((char)by);
	 }
	} catch (FileNotFoundException e) {
		
		e.printStackTrace();
	} catch (IOException e) {
		
		e.printStackTrace();
	}finally {
		try {
			if(fis!=null) {
				fis.close();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

int read(byte[] b):一次讀取一個位元組陣列。

FileInputStream fis = null;
byte[] b = new byte[1024];
try {
	fis = new FileInputStream("a.txt");
	int len = 0;
	while((len = fis.read(b))!=-1) {
		System.out.print(new String(b,0,len));
	}
} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException 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();
	}
}

3.複製檔案。 若資料來源 目的地是相同的,那麼複製的檔案必須要重新命名,因為不重新命名叫覆蓋。 資料來源:從哪裡來,即讀取檔案資料。 目的地:到哪裡去,即將讀取出的檔案資料寫入目標檔案中。 例子:

FileInputStream fis = null; 
	FileOutputStream fos = null;
	byte[] bs = new byte[1024];
		try {
			fis = new FileInputStream("a.txt");//讀取資料(輸入流)
			fos = new FileOutputStream("b.txt");//寫入資料(輸出流)
			int len = 0;
			//邊讀邊寫
			while((len = fis.read(bs)) != -1) {
				fos.write(bs);
			}
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}finally {
			try {
				fos.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}