字节缓冲流实现文件拷贝

/**
 * 字节缓冲流实现文件拷贝
 * 
 * 1.创建源 2.选择流 3.选择操作 4.关闭流
 * 
 * @author breeziness
 *
 */
public class IODemo04 {

	public static void main(String[] args) {
		bufferCopy("abc.txt", "abc-copy-2.txt");
	}

	public static void bufferCopy(String source, String dest) {
		InputStream is = null;
		OutputStream os = null;
		BufferedInputStream bi = null;
		BufferedOutputStream bo = null;
		int temp = 0;
		try {
			is = new FileInputStream(source);
			bi = new BufferedInputStream(is);
			os = new FileOutputStream(dest);
			bo = new BufferedOutputStream(os);

			while ((temp = bi.read()) != -1) {
				bo.write(temp);
				bo.flush();
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally { // 先打开的后关闭
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (bi != null) {
					bi.close();
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (os != null) {
					os.close();
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (bo != null) {
					bo.close();
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}

猜你喜欢

转载自blog.csdn.net/qq_40731414/article/details/86531132