Java FileInputStream Read readFile(File file, OutputStream output)

Here you can find the source of readFile(File file, OutputStream output)

Description

read File

License

Open Source License

Declaration

public static int readFile(File file, OutputStream output) 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.InputStream;

import java.io.OutputStream;

public class Main {
    public static int readFile(File file, OutputStream output) throws IOException {
        FileInputStream fis = null;

        try {/*from  ww w.ja  v  a 2  s.c om*/
            fis = new FileInputStream(file);

            return copy(fis, output);
        } finally {
            fis.close();
        }

    }

    public static int copy(InputStream input, OutputStream output) throws IOException {
        byte[] tempBuff = new byte[1024];
        int readCnt;
        int totalRead = 0;
        while (true) {
            readCnt = input.read(tempBuff, 0, tempBuff.length);

            if (readCnt < 0) {
                break;
            }

            totalRead += readCnt;

            if (readCnt > 0) {
                output.write(tempBuff, 0, readCnt);
                output.flush();
            }
        }

        return totalRead;
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file, boolean compress)
  5. readFile(File file, int size)
  6. readFile(File file, String encoding)
  7. readFile(File file, String encoding)
  8. readFile(File file, String encoding)
  9. readFile(File fyl)