---
title: "copy文件"
author: "peak007"
date: "2011-08-15T22:23:01Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "copy"
draft: false
lastmod: "2011-08-15T22:23:01Z"
id: 1149961
url: "https://peak007.iteye.com/blog/1149961"
---

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyPic {

	public void copy(String src, String dest) {// **********
		InputStream is = null;
		OutputStream os = null;
		char ch = src.toCharArray();

		// ************新添加的代码**********
		int pos = 0;
		for (int i = ch.length - 1; i >= 0; i--) {
			if (ch* == '\\') {
				if (i > pos)
					pos = i;
			}
		}
		String temp = src.substring(pos);
		dest = dest + temp;
		System.out.println("dest=" + dest);
		// ****************************************

		try {
			is = new BufferedInputStream(new FileInputStream(src));
			os = new BufferedOutputStream(new FileOutputStream(dest));

			byte b = new byte;
			int len = 0;
			String str = null;
			StringBuilder sb = new StringBuilder();
			try {
				while ((len = is.read(b)) != -1) {
					os.write(b, 0, len);

				}
				os.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}


}