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:com.sm.store.TestPhp.java

public static byte[] readFile(String filename) throws IOException {
    File file = new File(filename);
    long size = file.length();
    byte[] data = new byte[(int) size];
    FileInputStream fip = new FileInputStream(file);
    int len = fip.read(data);
    if (len != data.length)
        throw new RuntimeException("size expect " + data.length + " get " + len);
    else//from w  ww  .jav a2s  .  c o m
        return data;
}

From source file:Main.java

public static String readFromFile(String path) {
    String str = null;//  w  w w . j a  v a 2  s.c om
    if (path == null || path.trim().length() == 0) {
        return null;
    }

    File file = new File(path);

    if (file.exists() && file.isFile()) {
        int filelength = (int) file.length();
        byte[] filecontent = new byte[filelength];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
            str = new String(filecontent, "UTF-8");

        } catch (Exception e) {
            Log.v(TAG, "read file is error");
            return null;
        }

    }
    return str;
}

From source file:Main.java

public static byte[] readBytesFromFile(File file) {
    if (file != null && file.exists() && file.isFile()) {
        int filelength = (int) file.length();
        byte[] filecontent = new byte[filelength];
        try {//from   w w  w  .  j  av  a 2s  . co m
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();

        } catch (Exception e) {
            Log.v(TAG, "read file is error");
            return null;
        }
        return filecontent;
    }
    return null;
}

From source file:Main.java

public static String convertXmlToString(String xmlFileName) throws Exception {
    File file = new File(xmlFileName);
    FileInputStream insr = new FileInputStream(file);
    byte[] fileBuffer = new byte[(int) file.length()];
    insr.read(fileBuffer);
    insr.close();//from w  w w. j a va  2  s . co m
    return new String(fileBuffer);
}

From source file:Main.java

public static byte[] readBytesFromFile(String path) {
    if (path == null || path.trim().length() == 0) {
        return null;
    }//from   ww w .java2  s .  c o  m

    File file = new File(path);

    if (file.exists() && file.isFile()) {
        int filelength = (int) file.length();
        byte[] filecontent = new byte[filelength];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();

        } catch (IOException e) {
            Log.v(TAG, "read file is error");
            return null;
        }
        return filecontent;
    }
    return null;
}

From source file:Main.java

public static String encodeBase64File(String path) throws Exception {
    File file = new File(path);
    FileInputStream inputFile = new FileInputStream(file);
    byte[] buffer = new byte[(int) file.length()];
    inputFile.read(buffer);
    inputFile.close();/*from ww w .j  av a 2  s  .  com*/
    return android.util.Base64.encodeToString(buffer, Base64.DEFAULT);
}

From source file:Main.java

public static String FiletoBase64(String path) throws Exception {
    File file = new File(path);
    FileInputStream inputFile = new FileInputStream(file);
    byte[] buffer = new byte[(int) file.length()];
    inputFile.read(buffer);
    inputFile.close();/* w w  w  . j  a  v  a  2 s. c  o  m*/
    return new String(Base64.encodeBase64(buffer));
}

From source file:Main.java

public static String encodeBase64File(String path) throws Exception {
    File file = new File(path);
    FileInputStream inputFile = new FileInputStream(file);
    byte[] buffer = new byte[(int) file.length()];
    inputFile.read(buffer);
    inputFile.close();/*  ww w .  j  a  v a  2 s  .  com*/
    return Base64.encodeToString(buffer, Base64.DEFAULT);
}

From source file:Main.java

/**
 * This method reads an XML file and returns its contents as String.
 * @param xmlFileName The name (path) of the XML file.
 * @return The string (XML) representation of the XML file.
 * @throws Exception If there is a problem.
 *///from  w w  w . j ava 2  s  .  c om
public static String convertXmlToString(String xmlFileName) throws Exception {
    File file = new File(xmlFileName);
    FileInputStream insr = new FileInputStream(file);
    byte[] fileBuffer = new byte[(int) file.length()];
    insr.read(fileBuffer);
    insr.close();
    return new String(fileBuffer);
}

From source file:Main.java

public static byte[] getBytesFromFile(String path) throws IOException {
    FileInputStream file = new FileInputStream(path);
    byte[] data = new byte[(int) file.available()];
    file.read(data);
    file.close();//  w w w .  j  av a 2  s.c  o m
    return data;

}