Example usage for java.io FileInputStream read

List of usage examples for java.io FileInputStream read

Introduction

In this page you can find the example usage for java.io FileInputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:net.firejack.platform.core.utils.SecurityHelper.java

public static byte[] read64(String name) throws IOException {
    File file = new File(name);
    FileInputStream stream = new FileInputStream(file);
    byte[] bytes = new byte[(int) file.length()];
    stream.read(bytes);
    stream.close();//from   w  w w  .  ja v  a2s .c om
    return Base64.decode(bytes);
}

From source file:info.papdt.blacklight.support.http.FeedbackUtility.java

private static String readLog() {
    String res = "";

    try {//from w  w w .  j  ava  2  s . com
        FileInputStream fin = new FileInputStream(CrashHandler.CRASH_LOG);

        int length = fin.available();

        byte[] buffer = new byte[length];

        fin.read(buffer);

        res = EncodingUtils.getString(buffer, "UTF-8");

        fin.close();

    } catch (Exception e) {

        e.printStackTrace();
        return "";

    }
    return res;
}

From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.util.ZipUtils.java

private static byte[] readBytes(File file) {
    FileInputStream in = null;
    final byte buffer[] = new byte[(int) file.length()];
    try {//from   w  w  w  .  jav  a2  s  .  c om
        in = new FileInputStream(file);
        in.read(buffer);
    } catch (final IOException e) {
        MSGS.format(ERROR_READING_FILE_1, file.toString(), e);
    }
    if (in != null) {
        try {
            in.close();
        } catch (final IOException e) {
            // fail quietly
        }
    }
    return buffer;
}

From source file:Main.java

private static String readInStream(FileInputStream inStream) {
    try {//from   w  w  w .j  ava 2  s. co  m
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length = -1;
        while ((length = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, length);
        }

        outStream.close();
        inStream.close();
        return outStream.toString();
    } catch (IOException e) {
        Log.i("FileTest", e.getMessage());
    }
    return null;
}

From source file:Main.java

public static void copyFile(File fromFile, File toFile, Boolean rewrite) {
    if (!fromFile.exists()) {
        return;/*from w  w  w  .j av a 2  s.  c  o m*/
    }
    if (!fromFile.isFile()) {
        return;
    }
    if (!fromFile.canRead()) {
        return;
    }
    if (!toFile.getParentFile().exists()) {
        toFile.getParentFile().mkdirs();
    }
    if (toFile.exists() && rewrite) {
        toFile.delete();
    }

    try {
        java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);
        java.io.FileOutputStream fosto = new FileOutputStream(toFile);
        byte bt[] = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static byte[] getBytesFromFile(File file) {
    byte[] buffer = null;
    try {//w  w  w  .  j av  a2s. c o m
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

public static byte[] getBytesFromFile(String fileFullPath) {
    byte[] buffer = null;
    try {//from w  w  w  . java  2s. co  m
        File file = new File(fileFullPath);
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

private static String readInStream(FileInputStream inStream) {
    try {/*  w  ww  .j  a  va2  s .c om*/
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length = -1;
        while ((length = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, length);
        }

        outStream.close();
        inStream.close();
        return outStream.toString();
    } catch (IOException e) {
        // UIHelper.Log("e", "", "FileReadError", true);
    }
    return null;
}

From source file:Main.java

public static String read(String wbPath) {
    File file = new File(wbPath);
    if (file.exists()) {
        FileInputStream fis = null;
        try {//from w ww  .jav a2s .  c o  m
            fis = new FileInputStream(file);
            int len = fis.available();
            if (len > 0) {
                byte[] buf = new byte[len];
                fis.read(buf);
                String string = new String(buf, CHARSET);
                return string;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    return null;
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditingTestsUtils.java

static String readFile(File file) {
    try {/*from  w w  w .  jav  a2s .com*/
        FileInputStream input = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        input.read(data);
        input.close();
        return new String(data);
    } catch (Exception exception) {
        throw new AuditingException(exception, "Error reading file '%s' content.", file.getAbsolutePath());
    }
}