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[]readFileToBytes(File f)
read File To Bytes
int fLen = (int) f.length();
byte[] res = new byte[fLen];
FileInputStream fis = new FileInputStream(f);
fis.read(res);
fis.close();
return res;
byte[]readFileToBytes(File f, int max)
Read bytes from a file.
int length = (int) f.length();
if (max > 0 && max < length) {
    length = max;
try {
    return readStreamToBytes(new FileInputStream(f), length);
} catch (IOException ioe) {
    throw new IOException("could not read bytes from " + f.getAbsolutePath(), ioe);
...
byte[]readFileToBytes(String path)
Read file contents into byte array, which is suitable for small files.
File file = new File(path);
FileInputStream fin = new FileInputStream(path);
if (file.length() >= Integer.MAX_VALUE) {
    System.out.println("File size is too big. Please use another function to read the contents!");
    return null;
byte[] contents = new byte[(int) (file.length())];
fin.read(contents);
...
StringreadFileToStr(String fn, String charset)
read File To Str
FileInputStream in = null;
try {
    in = new FileInputStream(fn);
    try {
        byte[] buf = new byte[in.available()];
        int r = in.read(buf);
        return charset == null ? new String(buf, 0, r) : new String(buf, 0, r, charset);
    } finally {
...
StringreadFileUTF8(String file)
read File UTF
StringBuffer buffer = new StringBuffer();
try {
    DataInputStream dis = new DataInputStream(new FileInputStream(file));
    byte b[] = new byte[1];
    while (dis.available() > 0) {
        dis.read(b);
        String s = new String(b, "UTF-8");
        buffer.append(s);
...
StringreadFileUTF_8(String filePath)
read File UT_
String templetContent = "";
try {
    FileInputStream fileinputstream = new FileInputStream(filePath);
    int length = fileinputstream.available();
    byte bytes[] = new byte[length];
    fileinputstream.read(bytes);
    fileinputstream.close();
    templetContent = new String(bytes, "UTF-8");
...
voidwriteFile(File file, byte[] buffer, ZipOutputStream zos)
write File
try {
    try (FileInputStream fis = new FileInputStream(file)) {
        ZipEntry entry = new ZipEntry(file.getName());
        zos.putNextEntry(entry);
        int len;
        while ((len = fis.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        fis.close();
} catch (IOException ioe) {
voidwriteFile(InputStream fileInputStream, OutputStream outputStream)
write File
try {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = fileInputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    fileInputStream.close();
    outputStream.flush();
...
voidwriteFile(OutputStream os, File f)
write File
try (InputStream fis = new FileInputStream(f)) {
    writeFile(os, fis);
voidwriteFile(String path, OutputStream out)
write File
File file = new File(path);
if (file.exists() && !file.isDirectory()) {
    out.write(("HTTP/1.1 200 OK" + "\r\n" + "Content-Type: text/html" + "\r\n" + "Content-Length: "
            + file.length() + "\r\n" + "\r\n").getBytes());
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
    while (true) {
        int data = in.read();
        if (data == -1)
...