1. 程式人生 > >javaSE (三十)IO流異常處理、圖片加密處理、輸入檔案路徑並拷貝檔案內容、在檔案中鍵入內容

javaSE (三十)IO流異常處理、圖片加密處理、輸入檔案路徑並拷貝檔案內容、在檔案中鍵入內容

1、IO流異常處理:

IO流異常處理一般可以寫得如下這麼複雜(面試備用)

  • alt + shift + z 直接try-catch ,不過沒有加finally
  • 因為作用域的問題,需要在外面建立BufferedInputStream物件並且初始化為null(要是不初始化,就不需要關閉了)
  • 最後關閉的時候,需要來個巢狀try-catch,去日報關閉一個

程式碼中try-finally,沒加catch(最好還是加吧,反正可以快捷鍵):
java中異常處理時為什麼可以只要try-finally,而可以不要catch
捕獲異常,不處理,直接finally掃尾

package cn.njupt;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class file {
	public static void main(String[] args) throws IOException {

		BufferedInputStream bin =
null; BufferedOutputStream bout = null; try { bin = new BufferedInputStream(new FileInputStream("xxx.txt")); bout = new BufferedOutputStream(new FileOutputStream("yyy.txt")); int b = 0; while ((b = bin.read()) != -1) { bout.write(b); } } finally { // alt + shift + z 直接try-catch ,不過沒有加finally
try { if (bin != null) bin.close(); } finally { if (bout != null) bout.close(); } } } }

1.7新版本的IO異常處理:

因為IO流實現了 AutoCloseable的介面,所以可以自動關閉,要是想在try()的括號裡寫方法,需要實現實現介面(感覺這麼寫雖然簡潔但是沒什麼章法)

package cn.njupt;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class file {
	public static void main(String[] args) throws IOException {

		try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream("xxx.txt"));
			BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("yyy.txt"));)
		{		
			int b = 0;
			while ((b = bin.read()) != -1) {
				bout.write(b);
			}
	}
		
	}

}

2、圖片加密處理:

可以在write()的時候直接異或一個數,然後再次拷貝的時候需要再次異或同一個數(a異或b兩次等於a本身)

package cn.njupt;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class file {
	public static void main(String[] args) throws IOException {

		BufferedInputStream bin = new BufferedInputStream(new FileInputStream("yyy.txt"));
		BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("zzz.txt"));

		int b = 0;
		while ((b = bin.read()) != -1) {
			bout.write(b ^ 223);
		}

		bin.close();
		bout.close();
	}

}

3、輸入檔案路徑並拷貝:

  1. 鍵盤輸入 (加一個while迴圈鍵入和判斷)
  2. 判斷是否為檔案 File.isFile()
  3. IO流拷貝
package cn.njupt;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;


public class file {
	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);

		while (true) {
			System.out.println("請輸入正確的檔名:");
			String line = sc.nextLine();
			File file = new File(line);

			if (file.isFile()) {
				FileInputStream in = new FileInputStream(file);
				FileOutputStream out = new FileOutputStream(file.getName());

				byte[] arr = new byte[1024 * 8];
				int len;
				while ((len = in.read(arr)) != -1) {
					out.write(arr, 0, len);
				}

				in.close();
				out.close();
				break;

			} else if(file.isDirectory()){
				System.out.println("輸入的是資料夾,請重新輸入");
			}else if(!file.exists()){
				System.out.println("不存在此路徑,請重新輸入");
			}

		}

	}

}

4、在檔案中鍵入內容(遇到quit結束):

package cn.njupt;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class file {
	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);
		FileOutputStream out = new FileOutputStream("yyy.txt");
		while (true) {
			System.out.println("請輸入想要儲存的資料:");
			String line = sc.nextLine();
			if (line.equals("quit")) {
				break;
			} else {
				byte[] arr = line.getBytes();
				out.write(arr);
				out.write("\r\n".getBytes());//鍵入換行
				
			}

		}
			out.close();
	}

}

輸出:
我
是
吳彥祖