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:Main.java

public static String getFileMacEncrypt(File file, SecretKey key) throws IOException {
    String algorithm = key.getAlgorithm();
    Mac mac = null;//  ww  w.  j  a v a2s.  c o m
    try {
        mac = Mac.getInstance(algorithm);
        mac.init(key);
        FileInputStream in = new FileInputStream(file);
        byte[] buffer = new byte[1024 * 1024];
        int len = 0;
        while ((len = in.read(buffer)) > 0) {
            mac.update(buffer, 0, len);
        }
        in.close();
        return bytes2String(mac.doFinal());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String readTextFromSDcard(String fileName) {

    File file = new File(fileName);
    if (!file.exists()) {
        return null;
    }//from   w  ww  .  j ava 2  s.  c o  m
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        int availableLength = fileInputStream.available();
        byte[] buffer = new byte[availableLength];
        fileInputStream.read(buffer);
        fileInputStream.close();

        return new String(buffer, "UTF-8");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.domnian.BotConfiguration.java

public static void load(File file) throws Exception {
    if (file.createNewFile()) {
        BotConfiguration.writeDefault(file);
    }//  w ww .  jav a 2s.c om
    FileInputStream fis = new FileInputStream(file);
    byte[] buffer = new byte[1024];
    fis.read(buffer);
    JSONObject configJson = new JSONObject(new String(buffer));
    connectJson = configJson.getJSONObject("connect");
    identJson = configJson.getJSONObject("ident");
    nickServJson = configJson.getJSONObject("auth");
    manage = configJson.getString("manage");
}

From source file:Main.java

public static byte[] getBytesFromFile(File f) {
    if (f == null) {
        return null;
    }//from   w w w  . j a v  a 2s .  c o m
    try {
        FileInputStream stream = new FileInputStream(f);
        ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        for (int n; (n = stream.read(b)) != -1;) {
            out.write(b, 0, n);
        }
        stream.close();
        out.close();
        return out.toByteArray();
    } catch (IOException e) {
    }
    return null;
}

From source file:Main.java

public static byte[] readFileToByteArray2(File file) {
    try {//from   w  ww. ja  v  a 2s  .  com
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        bos.flush();
        byte[] bytes = bos.toByteArray();
        fis.close();
        bos.close();
        return bytes;

    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static void copyFile(File fromFile, File toFile) throws IOException {
    FileInputStream inputStream = new FileInputStream(fromFile);
    FileOutputStream outputStream = new FileOutputStream(toFile);
    byte[] buffer = new byte[4096];
    for (int numRead; (numRead = inputStream.read(buffer)) != -1;)
        outputStream.write(buffer, 0, numRead);
    inputStream.close();/*  ww  w . ja  v a2s.  c  om*/
    outputStream.close();
}

From source file:Main.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip, false);
    } else {/*from w  w  w. j  a  v  a 2 s  .c o m*/
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        try {
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            in.close();
        }
    }
}

From source file:Main.java

public static byte[] File2byte(String filePath) {
    byte[] buffer = null;
    try {/*from  w  w w .  j a v  a2 s .  c  om*/
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        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 void zipMutiCompress(File[] srcfile, File destFile) {
    byte[] buf = new byte[BUFFERED_SIZE];
    try {/*from   w  ww  .  ja va 2 s  .  c  om*/
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile));
        if (null != srcfile && srcfile.length > 0) {
            for (int i = 0; i < srcfile.length; i++) {
                File file = srcfile[i];
                if (null != file) {
                    FileInputStream in = new FileInputStream(file);
                    out.putNextEntry(new ZipEntry(file.getName()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.closeEntry();
                    in.close();
                }
            }
        }
        out.close();
    } catch (IOException e) {

    }
}

From source file:Main.java

/**
 * Copies source file to destination.//from  www. j  a  va2s.  c om
 * If destination file exists it will be overwritten silently.
 *
 * @param source file
 * @param target file
 */
public static void copyFile(final File source, final File target) {
    try {
        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(target);
        byte[] buf = new byte[4096];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}