Java Utililty Methods FileInputStream Read

List of utility methods to do FileInputStream Read

Description

The list of methods to do FileInputStream Read are organized into topic(s).

Method

byte[]readFile(File file)
read File
InputStream stream = new FileInputStream(file);
try {
    return readStream(stream);
} finally {
    stream.close();
byte[]readFile(File file)
read File
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[2048];
int read;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((read = fis.read(buf)) != -1) {
    bos.write(buf, 0, read);
fis.close();
...
byte[]readFile(File file)
Reads the content of a file (fully)
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
long length = file.length();
if (length > Integer.MAX_VALUE) {
    bis.close();
    throw new IOException("File is too large to read " + file.getName());
byte[] bytes = new byte[(int) length];
...
StringreadFile(File file)
read File
FileInputStream fstream = null;
try {
    fstream = new FileInputStream(file);
    byte[] bytes = new byte[(int) file.length()];
    fstream.read(bytes);
    fstream.close();
    return new String(bytes);
} catch (Exception e) {
...
StringreadFile(File file)
read File
if (!file.exists()) {
    return null;
ByteArrayOutputStream arrayOutputStream = null;
try {
    FileInputStream is = new FileInputStream(file);
    byte[] bytes = new byte[1024];
    arrayOutputStream = new ByteArrayOutputStream();
...
byte[]readFile(File file)
Read a file fully into a new byte array.
int length = (int) file.length();
byte[] b = new byte[length];
FileInputStream fis = null;
try {
    fis = new FileInputStream(file);
    while (length > 0) {
        length -= fis.read(b, b.length - length, length);
    return b;
} finally {
    fis.close();
StringreadFile(File file)
read File
FileInputStream in = null;
try {
    in = new FileInputStream(file);
    byte[] buf = new byte[in.available()];
    in.read(buf);
    return new String(buf, "UTF-8");
} finally {
    closeQuietly(in);
...
StringreadFile(File file)
reads the file specified in to a string.
try {
    if (file.length() > (5 * 1000 * 1000))
        throw new RuntimeException("the file is " + file.length()
                + " bytes. that is too large. it can't be larger than 5000000 bytes.");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileInputStream fis = new FileInputStream(file);
    copy(fis, baos);
    fis.close();
...
StringreadFile(File file)
read File
int nread;
byte buf[] = new byte[8192];
String content = "";
FileInputStream fis = new FileInputStream(file);
while ((nread = fis.read(buf)) != -1) {
    content += new String(buf, 0, nread);
fis.close();
...
byte[]readFile(File file, boolean compress)
Reads a file's contents into a byte array.
FileInputStream in = new FileInputStream(file);
try {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    OutputStream out;
    if (compress) {
        out = new GZIPOutputStream(buffer);
    } else {
        out = buffer;
...