Java FileChannel Copy copyFile(InputStream is, OutputStream os)

Here you can find the source of copyFile(InputStream is, OutputStream os)

Description

copy File

License

Open Source License

Parameter

Parameter Description
is a parameter
os a parameter

Exception

Parameter Description

Declaration

public static void copyFile(InputStream is, OutputStream os) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w ww .ja  v a 2 s.  co m*/
     
Copyright (C)    2007 Joao F. (joaof@sourceforge.net)
             http://paccman.sourceforge.net 
    
This program is free software; you can redistribute it and/or modify      
it under the terms of the GNU General Public License as published by      
the Free Software Foundation; either version 2 of the License, or         
(at your option) any later version.                                       
    
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 General Public License for more details.                              
    
You should have received a copy of the GNU General Public License         
along with this program; if not, write to the Free Software               
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
     
*/

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;

public class Main {
    private static final int BUF_SIZE = 256 * 1024;

    /**
     * 
     * @param is
     * @param os
     * @throws java.io.IOException
     */
    public static void copyFile(InputStream is, OutputStream os) throws IOException {
        byte[] buf = new byte[BUF_SIZE];
        int len;
        while ((len = is.read(buf)) > 0) {
            os.write(buf, 0, len);
        }
        is.close();
        os.close();
    }

    /**
     * Copy <code>inputFile</code> to <code>outputFile</code>
     * @param inputFile The source file.
     * @param outputFile The destination file.
     * @throws java.io.IOException
     */
    public static void copyFile(File inputFile, File outputFile) throws IOException {
        // Create channel on the source
        FileChannel srcChannel = new FileInputStream(inputFile).getChannel();

        // Create channel on the destination
        FileChannel dstChannel = new FileOutputStream(outputFile).getChannel();

        // Copy file contents from source to destination
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

        // Close the channels
        srcChannel.close();
        dstChannel.close();
    }
}

Related

  1. copyFile(final File src, final File dst)
  2. copyFile(final File srcFile, final File destFile)
  3. copyFile(final File srcFile, final File destFile)
  4. copyFile(final String src, final String dest)
  5. copyFile(final String src, final String dst)
  6. copyFile(java.io.File fromFile, java.io.File toFile)
  7. copyFile(java.io.File in, java.io.File out)
  8. copyFile(Path source, Path target)
  9. copyFile(String fromFileName, String toFileName)