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)
read File
return readFile(new File(filename));
StringreadFile(String filename)
read File
String fileContents = "";
File file = new File(filename);
if (!file.exists() || !file.isFile())
    throw new IllegalArgumentException();
try (FileInputStream stream = new FileInputStream(file)) {
    byte[] fileBytes = new byte[(int) file.length()];
    stream.read(fileBytes, 0, (int) file.length());
    fileContents = new String(fileBytes);
...
ObjectreadFile(String filename)
read File
FileInputStream fis = null;
ObjectInputStream ois = null;
Object o = null;
try {
    fis = new FileInputStream(DATA_DIR + filename);
    ois = new ObjectInputStream(fis);
    try {
        o = ois.readObject();
...
StringreadFile(String fileName)
read File
try {
    File file = new File(fileName);
    BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
    byte[] bytes = new byte[(int) file.length()];
    stream.read(bytes);
    return new String(bytes);
} catch (Exception e) {
    throw new RuntimeException(e);
...
StringreadFile(String fileName, String charset)
read File
File file = new File(fileName);
return readFile(file, charset);
StringreadFile(String filePath)
read File
byte[] data = new byte[0];
FileInputStream fileInputStream = null;
try {
    File file = new File(filePath);
    fileInputStream = new FileInputStream(new File(filePath));
    data = new byte[(int) file.length()];
    fileInputStream.read(data);
} finally {
...
byte[]readFile(String filePath)
read File
FileInputStream fis = new FileInputStream(new File(filePath));
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
return bytes;
byte[]readFile(String filePath)
Read a binary file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(filePath));
byte[] buffer = new byte[1024];
int cant = fis.read(buffer);
while (cant > 0) {
    baos.write(buffer, 0, cant);
    cant = fis.read(buffer);
fis.close();
return baos.toByteArray();
StringreadFile(String filePath)
read File
File file = new File(filePath);
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
while (true) {
    int content = fin.read();
    if (content == -1) {
        String content1 = new String(bs.toByteArray(), "UTF-8");
        fin.close();
...
byte[]readFile(String filePath)
read file to byte[] with specified file path
File infoFile = new File(filePath);
byte[] result = null;
if (infoFile.exists()) {
    result = new byte[(int) infoFile.length()];
    try {
        FileInputStream fis = new FileInputStream(infoFile);
        fis.read(result);
        fis.close();
...