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 readSDFile(String fileName) throws IOException {

    File file = new File(fileName);

    FileInputStream fis = new FileInputStream(file);

    int length = fis.available();

    byte[] buffer = new byte[length];
    fis.read(buffer);

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

    fis.close();//from   w w  w  .  jav a2  s . c  o  m
    return res;
}

From source file:Main.java

public static byte[] getFileByte(File file) {
    if (!file.exists()) {
        return null;
    }// w  w  w . j av  a2 s  .com
    try {
        FileInputStream fis = new FileInputStream(file);
        int len = fis.available();
        byte[] bytes = new byte[len];
        fis.read(bytes);
        fis.close();
        return bytes;
    } catch (Exception e) {

    }

    return null;
}

From source file:Main.java

public static byte[] readBytes(String file) {
    try {//from   w w w .j a v  a  2s  .co  m
        FileInputStream fis = new FileInputStream(file);
        int len = fis.available();
        byte[] buffer = new byte[len];
        fis.read(buffer);
        fis.close();
        return buffer;
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return null;

}

From source file:Main.java

public static byte[] read(Context context, String fileName) throws FileNotFoundException, IOException {

    File file = getFile(context, fileName);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    if (file != null) {
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[fis.available()];
        fis.read(data);
        bos.write(data);//from  www  .  j  a  v  a 2s  . c o  m
        fis.close();
    }

    return bos.toByteArray();
}

From source file:Main.java

/**
 * Read data from file./*from   w ww.  j  av  a 2 s.c o  m*/
 *
 * @param fileName
 * @return
 */
public static String readFile(String fileName) {
    String ret = "";
    try {
        FileInputStream fin = new FileInputStream(fileName);
        int length = fin.available();
        byte[] buffer = new byte[length];
        fin.read(buffer);
        //         ret = EncodingUtils.getString(buffer, "UTF-8");
        ret = buffer.toString();
        fin.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:Main.java

public static boolean copy(File source, File target) {
    if (source == null || target == null || !source.exists() || source.length() < 100) {
        return false;
    }//from w w w .  j a v  a  2  s  .  co m
    try {
        FileOutputStream fos = new FileOutputStream(target);
        FileInputStream fis = new FileInputStream(source);
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        fos.flush();
        fos.close();
        fis.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

/**
 * @param fileName//from w ww.  j a  v  a  2  s.c  o  m
 */
public static String readFileByLines(String fileName) {
    String res = "";
    try {
        FileInputStream fin = new FileInputStream(fileName);
        int length = fin.available();
        byte[] buffer = new byte[length + 1024];
        fin.read(buffer);
        res = EncodingUtils.getString(buffer, "UTF-8");
        fin.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return res;
}

From source file:com.chargebee.MethodBank.MethodBank.java

public static JSONObject readJsonObjectData(String fileName) throws Exception { //processes the Json object and returns in the required format. 
    File f = new File(fileName);
    FileInputStream fr = new FileInputStream(f);
    byte[] b = new byte[fr.available()];
    fr.read(b);
    JSONObject jobj = new JSONObject(new String(b));
    return jobj; // The required Json object

}

From source file:com.chargebee.MethodBank.MethodBank.java

public static JSONArray readJsonArrayData(String fileName) throws Exception { //processes the Json array and returns in the required format. 
    File f = new File(fileName);
    FileInputStream fr = new FileInputStream(f);
    byte[] b = new byte[fr.available()];
    fr.read(b);
    JSONArray jarr = new JSONArray(new String(b));
    return jarr; // The required Json array

}

From source file:net.itransformers.bgpPeeringMap.BgpPeeringMapFromFile.java

private static byte[] readRawDataFile(String rawData) throws Exception {
    FileInputStream is = new FileInputStream(rawData);
    byte[] data = new byte[is.available()];
    is.read(data);
    return data;/*from w w w  . j  a v  a 2 s  . com*/
}