Java FileInputStream Copy copyFileToOutputStream(String fileLocation, OutputStream os)

Here you can find the source of copyFileToOutputStream(String fileLocation, OutputStream os)

Description

copy File To Output Stream

License

Apache License

Declaration

public static void copyFileToOutputStream(String fileLocation, OutputStream os) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    private static final int DEFAULT_BUFFER_SIZE = 1024;

    public static void copyFileToOutputStream(String fileLocation, OutputStream os) throws IOException {
        FileInputStream fis = new FileInputStream(fileLocation);
        copyStream(fis, os);//from   w  ww.  ja  v a  2  s. c  o  m
    }

    public static void copyStream(InputStream is, OutputStream os) throws IOException {
        if (is == null) {
            throw new IllegalArgumentException("InputStream is null");
        }

        if (os == null) {
            throw new IllegalArgumentException("OutputStream is null");
        }

        byte[] b = new byte[DEFAULT_BUFFER_SIZE];
        int len = 0;

        try {
            while ((len = is.read(b, 0, DEFAULT_BUFFER_SIZE)) != -1) {
                os.write(b, 0, len);
            }
        } finally {
            is.close();
            os.flush();
        }
    }
}

Related

  1. copyFileToDir(String targetDir, String[] filePath)
  2. CopyFileToDirectory(String file_name, String from_directory, String to_directory)
  3. copyFileToFile(File file, File destFile, boolean overwrite)
  4. copyFileToFile(File input, String outputPath, String exportName)
  5. copyFileToOutputStream(File file, OutputStream stream)
  6. copyFileToStream(File currentFile, OutputStream outputStream)
  7. copyFileToStream(File file, OutputStream stream)
  8. copyFileToStream(File file, OutputStream stream)
  9. copyFileToString(java.io.File f)