JAvaでソースファイルの内容をターゲットファイルにコピーする方法


package io_test;

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.io.InputStream;
import java.io.OutputStream;

public class IoTest {
	public static void main(String[] args) {
		File fileSrcFile1 = new File("e:\\     .rar");
		File fileDestFile1 = new File("e:\\demo\\     .rar");
		File fileSrcFileF1 = new File("e:\\        .rar");
		File fileDestFileF1 = new File("e:\\demo\\        .rar");
		try {
			long start1 = System.currentTimeMillis();
			copyFile1(fileSrcFile1, fileDestFile1);
			long end1 = System.currentTimeMillis();
			System.out.println("copyFile1  :" + (end1 - start1) / 1000.0);

			long start2 = System.currentTimeMillis();
			copyFile2(fileSrcFileF1, fileDestFileF1);
			long end2 = System.currentTimeMillis();
			System.out.println("copyFile2  :" + (end2 - start2) / 1000.0);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 *               
	 * 
	 * @param src
	 *               
	 * @param dest
	 *                
	 */
	public static void copyFile1(File src, File dest) throws IOException {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		byte[] b = new byte[8192];
		try {
			bis = new BufferedInputStream(new FileInputStream(src));
			bos = new BufferedOutputStream(new FileOutputStream(dest));

			for (int count = -1; (count = bis.read(b)) != -1;) {
				bos.write(b, 0, count);
			}
			bos.flush();
		} catch (IOException e) {
			throw e;
		} finally {
			close(bis);
			close(bos);
		}
	}

	/**
	 *               
	 * 
	 * @param srcFile
	 *               
	 * @param destFile
	 *                
	 */
	public static void copyFile2(File srcFile, File destFile)
			throws IOException {
		FileInputStream input = new FileInputStream(srcFile);
		try {
			FileOutputStream output = new FileOutputStream(destFile);
			try {
				byte[] buffer = new byte[4096];
				int n = 0;
				while (-1 != (n = input.read(buffer))) {
					output.write(buffer, 0, n);
				}
			} finally {
				try {
					if (output != null) {
						output.close();
					}
				} catch (IOException ioe) {
					// ignore
				}
			}
		} finally {
			try {
				if (input != null) {
					input.close();
				}
			} catch (IOException ioe) {
				// ignore
			}
		}
	}

	/**
	 *            ,            RuntimeException
	 * 
	 * @param is
	 * @param os
	 */
	public static void close(InputStream is, OutputStream os) {
		close(is);
		close(os);
	}

	/**
	 *           ,            RuntimeException
	 * 
	 * @param is
	 */
	public static void close(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *           ,            RuntimeException
	 * 
	 * @param os
	 */
	public static void close(OutputStream os) {
		if (os != null) {
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}