Java FileInputStream Read readFile(File f)

Here you can find the source of readFile(File f)

Description

Utility method for reading a file, used for testing.

License

Open Source License

Declaration

static private byte[] readFile(File f) 

Method Source Code


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

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {
    /** Utility method for reading a file, used for testing. */
    static private byte[] readFile(File f) {
        // Get the content as a byte array, and compute its checksum
        byte[] content = new byte[0];
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
                ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            // Get a chunk at a time 
            byte[] buffer = new byte[8192]; // 8K
            int bytesRead;
            while ((bytesRead = in.read(buffer)) > 0) {
                out.write(buffer, 0, bytesRead);
            }//from  w  ww. j a v  a  2  s  . co  m

            content = out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
}

Related

  1. readFile( String filename)
  2. readFile(File f)
  3. readFile(File f)
  4. readFile(File f)
  5. readFile(File f)
  6. readFile(File f)
  7. readFile(File f)
  8. readFile(File f)
  9. readFile(File f)