博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

03_简单实现文件的拷贝

Posted on 2017-11-02 10:17  叶灬黎  阅读(190)  评论(0)    收藏  举报

demo需求:

简单实现文件的拷贝

demo主要技术:

FileInputStream/FileOutputStream文件输入/输出流的使用(文件拷贝一般只考虑字节流,因为什么文件都能拷贝)

demo主要代码展示:

.
    /**
	 * show 方法简介
	 * 将源文件拷贝至目标文件
	 * @author 叶灬黎
	 * @param sourceFile 源文件
	 * @param targetFile 拷贝后文件
	 * @return true/false
	 * @throws Exception
	 */
	public static boolean fileCopy(String sourceFile, String targetFile) throws Exception {
		boolean flag = false;

		FileInputStream fis;
		FileOutputStream fos;

		try {
			fis = new FileInputStream(new File(sourceFile));
			fos = new FileOutputStream(new File(targetFile));
		} catch (Exception e) {
			throw new Exception("文件路径有误");
		}

		byte[] bs = new byte[64];

		try {
			while (true) {
				int length = fis.read(bs, 0, bs.length);
				if (length == -1) {
					break;
				}
				fos.write(bs, 0, length);
			}
		} catch (Exception e) {
			throw new Exception("文件拷贝出错");
		}

		fos.close();
		fis.close();
		flag = true;
		return flag;
	}

demo资源位置:

 svn://106.15.229.200/Javaweb/tinyDemo_file_copy 用户temp/密码temp