Java InputStream Copy nio copy(InputStream src, File dst)

Here you can find the source of copy(InputStream src, File dst)

Description

Copy the content of a stream to a destination file

License

Open Source License

Parameter

Parameter Description
src the original stream
dst the destination path

Declaration

static public void copy(InputStream src, File dst) throws Exception 

Method Source Code


//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();
    }
}

Related

  1. copy(InputStream in, OutputStream out, boolean closeInput, boolean closeOutput)
  2. copy(InputStream input, OutputStream output)
  3. copy(InputStream input, OutputStream output)
  4. copy(InputStream input, OutputStream output)
  5. copy(InputStream input, OutputStream output)
  6. copy(InputStream stream, File file)
  7. copyStream(final InputStream aInStream, final OutputStream aOutStream)
  8. copyStream(final InputStream in)
  9. copyStream(final InputStream is, final OutputStream os)