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

StringreadFile(String filename)
Reads the contents of the file with the given name, returning a String.
StringBuffer sb = new StringBuffer(10000);
char[] cb = new char[10000]; 
int j = 0;
java.io.FileReader f = null;
try {
    f = new java.io.FileReader(filename);
    while (j != -1) {
        j = f.read(cb, 0, cb.length);
...
StringreadFile(File f)
read File
byte[] baselineBuffer = new byte[(int) f.length()];
FileInputStream s = new FileInputStream(f);
s.read(baselineBuffer);
s.close();
return new String(baselineBuffer);
byte[]readFile(File f)
read File
byte[] b = new byte[(int) f.length()];
DataInputStream in = new DataInputStream(new FileInputStream(f));
in.readFully(b);
in.close();
return b;
StringreadFile(File f)
Reads in the file's contents, skipping all line terminators (newlines or carriage returns).
BufferedInputStream bis = null;
try {
    bis = new BufferedInputStream(new FileInputStream(f));
    StringBuilder buf = new StringBuilder((int) f.length());
    int c;
    while (true) {
        c = bis.read();
        if (c == -1)
...
byte[]readFile(File f)
read File
DataInputStream in = new DataInputStream(new FileInputStream(f));
byte[] ret = new byte[(int) f.length()];
in.readFully(ret);
in.close();
return ret;
byte[]readFile(File f)
Utility method for reading a file, used for testing.
byte[] content = new byte[0];
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
        ByteArrayOutputStream out = new ByteArrayOutputStream()) {
    byte[] buffer = new byte[8192]; 
    int bytesRead;
    while ((bytesRead = in.read(buffer)) > 0) {
        out.write(buffer, 0, bytesRead);
    content = out.toByteArray();
} catch (IOException e) {
    e.printStackTrace();
return content;
byte[]readFile(File f)
read File
int len = f.length() > Integer.MAX_VALUE ? 8192 : (int) f.length();
byte[] buf = new byte[len];
FileInputStream is = new FileInputStream(f);
try {
    ByteArrayOutputStream os = new ByteArrayOutputStream((int) f.length());
    int n = is.read(buf);
    while (n != -1) {
        os.write(buf, 0, n);
...
byte[]readFile(File f)
read File
FileInputStream input = new FileInputStream(f);
byte[] fileBytes = readStream(input, true);
return fileBytes;
StringreadFile(File f)
read File
try (FileInputStream fis = new FileInputStream(f)) {
    byte[] data = new byte[(int) f.length()];
    fis.read(data);
    return new String(data, "UTF-8");
StringreadFile(File f)
read File
byte[] baselineBuffer = new byte[(int) f.length()];
new FileInputStream(f).read(baselineBuffer);
return new String(baselineBuffer);