Java InputStream to File writeStreamToFile(InputStream is, File file)

Here you can find the source of writeStreamToFile(InputStream is, File file)

Description

write Stream To File

License

Apache License

Declaration

protected static void writeStreamToFile(InputStream is, File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Geoscience Australia/*from   w w  w  .jav a 2s.c  o  m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    protected static void writeStreamToFile(InputStream is, File file) {
        byte[] buffer = new byte[512];
        BufferedOutputStream out = null;
        try {
            try {
                out = new BufferedOutputStream(new FileOutputStream(file));
                while (true) {
                    int read = is.read(buffer);
                    if (read < 0) {
                        break;
                    }
                    out.write(buffer, 0, read);
                }
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        } catch (IOException e) {
        }
    }
}

Related

  1. writeStreamToFile(InputStream in, OutputStream out)
  2. writeStreamToFile(InputStream in, String fileName)
  3. writeStreamToFile(InputStream inputStream, File file)
  4. writeStreamToFile(InputStream inputStream, File targetFile)
  5. writeStreamToFile(InputStream inputStream, String extension)
  6. writeStreamToFile(InputStream is, File out)
  7. writeStreamToFile(InputStream is, File toFile)
  8. writeStreamToFile(InputStream is, String fileName)
  9. writeStreamToFile(InputStream is, String path)