Here you can find the source of copy(InputStream src, File dst)
Parameter | Description |
---|---|
src | the original stream |
dst | the destination path |
static public void copy(InputStream src, File dst) throws Exception
//package com.java2s; /*/* w ww .j av a 2s. c o m*/ * EuroCarbDB, a framework for carbohydrate bioinformatics * * Copyright (c) 2006-2009, Eurocarb project, or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * A copy of this license accompanies this distribution in the file LICENSE.txt. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * Last commit: $Rev: 1930 $ by $Author: david@nixbioinf.org $ on $Date:: 2010-07-29 #$ */ import java.io.*; import java.nio.channels.FileChannel; public class Main { /** * Copy a file to a different destination * * @param src * the original file * @param dst * the destination path */ static public void copy(File src, File dst) throws Exception { if (src == null) throw new Exception("Invalid source file"); if (dst == null) throw new Exception("Invalid destination file"); FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { long position = 0; long size = inChannel.size(); while (position < size) position += inChannel.transferTo(position, 32000, outChannel); } catch (IOException e) { throw e; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } } /** * Copy the content of a stream to a destination file * * @param src * the original stream * @param dst * the destination path */ static public void copy(InputStream src, File dst) throws Exception { if (src == null) throw new Exception("Invalid source stream"); if (dst == null) throw new Exception("Invalid destination file"); BufferedInputStream bis = new BufferedInputStream(src); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dst)); int ch = -1; while ((ch = bis.read()) != -1) { bos.write(ch); } bos.close(); } }