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 readTextFile(String fileName) throws FileNotFoundException, IOException {
    File file = new File(fileName);
    FileInputStream in = new FileInputStream(file);
    byte buf[] = new byte[(int) file.length()];
    in.read(buf);
    return new String(buf);
}

From source file:Main.java

static String File2String(String path) {
    String strReturn = "";
    try {//  w w  w.  j a v  a  2  s  .  com
        File f = new File(path);
        FileInputStream fis = new FileInputStream(f);
        byte[] bDoc = new byte[fis.available()];
        fis.read(bDoc);
        strReturn = new String(bDoc);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return strReturn;
}

From source file:Main.java

public static void gzipIt(String inputFile, String outputFile) {

    byte[] buffer = new byte[1024];

    try {//from  ww w  . ja v  a  2s.  c  om

        GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outputFile));

        FileInputStream in = new FileInputStream(inputFile);

        int len;
        while ((len = in.read(buffer)) > 0) {
            gzos.write(buffer, 0, len);
        }

        in.close();

        gzos.finish();
        gzos.close();

        System.out.println("Done");

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:FolderZiper.java

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

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

From source file:Main.java

public static String encodeToFile(File file) {
    try {// ww w .  ja v  a2s  .c om
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return Base64.encodeToString(buffer, Base64.NO_WRAP);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] readBytes(File file) throws IOException {
    //check/* ww w. jav  a2  s  . c  o  m*/
    if (!file.exists()) {
        throw new FileNotFoundException("File not exist: " + file);
    }
    if (!file.isFile()) {
        throw new IOException("Not a file:" + file);
    }

    long len = file.length();
    if (len >= Integer.MAX_VALUE) {
        throw new IOException("File is larger then max array size");
    }

    byte[] bytes = new byte[(int) len];
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        in.read(bytes);
    } finally {
        close(in);
    }

    return bytes;
}

From source file:Main.java

/**
 * <pre>/* w ww.  jav a2  s . c  om*/
 * Read binary data from file stored in app 's internal memory.
 * </pre>
 * @param absolutePath absolute path to source file
 * @return binary data
 */
public static byte[] readInternalFile(String absolutePath) throws IOException {
    FileInputStream in = getCurrentContext().openFileInput(absolutePath);
    byte[] b = new byte[in.available()];
    in.read(b);
    in.close();

    return b;
}

From source file:id.co.nlp.MachineTranslation.Utils.Util.java

public static String read(String path) throws FileNotFoundException, IOException {
    File file = new File(path);
    long l = file.length();
    byte[] b = new byte[(int) l];
    FileInputStream fileInputStream = new FileInputStream(file);
    fileInputStream.read(b);
    return new String(b);
}

From source file:Main.java

public static void copyFile(FileInputStream src, FileOutputStream dst) throws Throwable {
    byte[] buf = new byte[65536];

    for (int len = src.read(buf); len > 0; len = src.read(buf)) {
        dst.write(buf, 0, len);//  w w  w.  ja  v a2 s .  c  o  m
    }

    src.close();
    dst.close();
}

From source file:Main.java

/**
 * <pre>/*from   w ww. java  2s  . co m*/
 * Read binary data from arbitrary file.
 * </pre>
 * @param absolutePath absolute path to source file
 * @return binary data
 */
public static byte[] readFile(String absolutePath) throws IOException {
    File file = new File(absolutePath);
    if (!file.exists()) {
        return null;
    }

    FileInputStream in = new FileInputStream(file);
    byte[] b = new byte[in.available()];
    in.read(b);
    in.close();

    return b;
}