1. 程式人生 > >使用IO流進行檔案的拷貝

使用IO流進行檔案的拷貝

直接上程式碼:

package com.yyj.IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class IOTest {
	public static void main(String[] args)   {
		String filePath = "E:\\IO.txt";
		File file = new File(filePath);
		if(file.exists()){
			System.out.println("檔案存在!");
		}
		FileInputStream is = null;
		FileOutputStream os = null;
		
		int temp =0;
		byte[] bytes = new byte[1024];
		
		try {
			is = new FileInputStream(filePath);
			os = new FileOutputStream("E:\\IOs.txt");
			while((temp = is.read(bytes))!=-1){
				os.write(bytes, 0, temp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}