java复制文件的4种方式

第一种方法:使用FileInputStream以及FileOutputStream读写文件(有方法注释,建议放到工具中查看)


	public void fileCopy() {
		FileInputStream input = null;
		FileOutputStream output = null;

		try {

			input = new FileInputStream(new File("文件路径"));
			output = new FileOutputStream(new File("目标文件路径"));

			byte[] bt = new byte[1024];
			int realbyte = 0;
			/**      input.read(bt)
		     * Reads up to <code>b.length</code> bytes of data from this input
		     * stream into an array of bytes. This method blocks until some input
		     * is available.
		     *
		     * @param      b   the buffer into which the data is read.
		     * @return     the total number of bytes read into the buffer, or
		     *             <code>-1</code> if there is no more data because the end of
		     *             the file has been reached.
		     * @exception  IOException  if an I/O error occurs.
		     */
			while ((realbyte = input.read(bt)) > 0) {
				
				/**   output.write(bt,0,realbyte)
			     * Writes <code>len</code> bytes from the specified byte array
			     * starting at offset <code>off</code> to this file output stream.
			     *
			     * @param      b     the data.
			     * @param      off   the start offset in the data.
			     * @param      len   the number of bytes to write.
			     * @exception  IOException  if an I/O error occurs.
			     */
				output.write(bt,0,realbyte);
			}
			
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (input != null) {
					input.close();
				}
				if (output != null) {
					output.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

第二种方法:效率比上一个高一些

public void fileCopy_channel() {
		FileChannel input = null;
		FileChannel output = null;

		try {
			input = new FileInputStream("givenfile").getChannel();
			output = new FileOutputStream("tofile").getChannel();
			/**
			 * Transfers bytes into this channel's file from the given readable byte channel.
			 *  @param  src
		     *         The source channel
		     *
		     * @param  position
		     *         The position within the file at which the transfer is to begin;
		     *         must be non-negative
		     *
		     * @param  count
		     *         The maximum number of bytes to be transferred; must be
		     *         non-negative
			 */
			output.transferFrom(input, 0, input.size());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (input != null) {
					input.close();
				}
				if (output != null) {
					output.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

第三种和第四种都是封装好的方法:

Apache Commons IO的方法FileUtils.copyFile(source, dest);

JDk以后才会有的Files.copy(source.toPath(), dest.toPath());

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值