Java FileChannel Copy copyStreamToFile(InputStream inStream, File file)

Here you can find the source of copyStreamToFile(InputStream inStream, File file)

Description

copy Stream To File

License

Open Source License

Declaration

public static void copyStreamToFile(InputStream inStream, File file) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

public class Main {
    public static void copyStreamToFile(InputStream inStream, File file) throws IOException {
        try (ReadableByteChannel in = Channels.newChannel(inStream)) {
            try (FileOutputStream outStream = new FileOutputStream(file)) {
                FileChannel out = outStream.getChannel();
                long offset = 0;
                long quantum = 1024 * 1024;
                long count;
                while ((count = out.transferFrom(in, offset, quantum)) > 0) {
                    offset += count;//from w ww . j av a  2  s . co m
                }
            }
        }
    }
}

Related

  1. copyInternal(FileInputStream in, FileOutputStream out)
  2. copyLarge(FileInputStream in, FileOutputStream out)
  3. copyNio(final File source_file, final File destination_file)
  4. copyRec(File src, File dst)
  5. copyStream(InputStream in, long length, File out)
  6. copyThrowsOnException(final File source, final File destination)
  7. copyTo(File srcFile, File destFile)
  8. copyToFolderAs(File fromFile, File folder, String asName)
  9. copyToTemp(File srcFile)