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

ListreadFile(String filename)
read File
List<Byte> result = new ArrayList<>();
try (FileInputStream file = new FileInputStream(filename)) {
    byte[] buffer = new byte[64 * 1024];
    int bytesRead = 0;
    while ((bytesRead = file.read(buffer)) > 0) {
        for (int i = 0; i < bytesRead; i++)
            result.add(buffer[i]);
} catch (IOException e) {
    throw new RuntimeException(e);
return result;
byte[]readFile(String fileName)
read File
try {
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));
    int n = in.available();
    byte[] t = new byte[n];
    int i = 0;
    while (in.available() != 0) {
        t[i] = in.readByte();
        i++;
...
byte[]readFile(String filename)
read File
File file = new File(filename);
long len = file.length();
byte data[] = new byte[(int) len];
FileInputStream fin = new FileInputStream(file);
int r = fin.read(data);
if (r != len) {
    throw new IOException("Only read " + r + " of " + len + " for " + file);
fin.close();
return data;
InputStreamreadFile(String fileName)
read File
InputStream result = null;
try {
    result = new java.io.FileInputStream(fileName);
} catch (FileNotFoundException ex) {
    throw new IllegalArgumentException(ex);
if (result == null) {
    throw new IllegalArgumentException("Impossible to open the file.");
...
byte[]readFile(String fileName)
Reads a file and return its content as a byte array.
return readFile(new File(fileName));
StringreadFile(String filename)
read File
File d = new File(filename);
FileInputStream fin = new FileInputStream(d);
byte b[] = readStream(fin);
fin.close();
return new String(b);
byte[]readFile(String filename)
read File
File file = new File(filename);
long len = file.length();
byte data[] = new byte[(int) len];
FileInputStream fin = new FileInputStream(file);
int r = fin.read(data);
if (r != len)
    throw new IOException("Only read " + r + " of " + len + " for " + file);
fin.close();
...
byte[]readFile(String fileName)
read File
FileInputStream in = new FileInputStream(fileName);
byte[] bytes = new byte[in.available()];
in.read(bytes);
in.close();
return bytes;
byte[]readFile(String filename)
Read a file into a buffer sized the size of the file
File file = new File(filename);
FileInputStream stream = new FileInputStream(file);
return readFromInputStream(stream);
Stringreadfile(String filename)
readfile
String ret = "";
File file = new File(filename);
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try {
    fin = new FileInputStream(file);
    while ((ch = fin.read()) != -1)
...