Java FileChannel Copy copyFileToStream(File in, OutputStream out)

Here you can find the source of copyFileToStream(File in, OutputStream out)

Description

copy File To Stream

License

Open Source License

Declaration

public static void copyFileToStream(File in, OutputStream out) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.io.IOException;

import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
    public static void copyFileToStream(File in, OutputStream out) throws IOException {
        FileInputStream is = new FileInputStream(in);
        FileChannel inChannel = is.getChannel();
        WritableByteChannel outChannel = Channels.newChannel(out);
        try {//from  w  ww. ja v a  2  s  .  co  m
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if (is != null)
                is.close();
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }
}

Related

  1. copyFiles(File srcFile, File dstFile, boolean overwrite)
  2. copyFiles(final File fromFile, final File toFile)
  3. copyFileToDir(File file, File destDir)
  4. copyFileToDirectory(String fileFrom, String destinationDirectory)
  5. copyFileToFolder(final String resultFile, final String targetPath)
  6. copyFileUsingChannel(File source, File dest)
  7. copyFileUsingChannel(File source, File dest)
  8. copyFileUsingFileChannels(File source, File dest)
  9. copyFileUsingFileChannels(File source, File dest)