Example usage for java.io FileInputStream available

List of usage examples for java.io FileInputStream available

Introduction

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

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

Usage

From source file:Main.java

public static byte[] readBytes(String file) {
    try {/*from   ww  w . ja  v a2  s  . c  o 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

/**
 * Read data from file./*  w w w . j a v  a 2s  . 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

private static long getFileSize(File file) throws Exception {
    long size = 0;
    if (file.exists()) {
        FileInputStream fis = null;
        fis = new FileInputStream(file);
        size = fis.available();
    } else {//from   ww w  .j a v a2s.  c  om
        file.createNewFile();
    }
    return size;
}

From source file:Main.java

static String File2String(String path) {
    String strReturn = "";
    try {/*  w  w  w . ja  v a2s.co  m*/
        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 byte[] getBytesFromFile(String path) throws IOException {
    FileInputStream file = new FileInputStream(path);
    byte[] data = new byte[(int) file.available()];
    file.read(data);/*  ww w  .  j a va  2 s  . c om*/
    file.close();
    return data;

}

From source file:Main.java

public static long getFileSizes(File f) {
    long s = 0;/*from   w  w w .j  a  v  a  2 s .c  o m*/
    try {
        if (f.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(f);
            s = fis.available();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return s;
}

From source file:Main.java

/**
 * Converts a file specified by the path, to the String.
 *//*from w w  w  . j  av a  2 s  .c  o  m*/
public static String fileToString(String fileName) {
    if (fileName != null) {
        //String sLine;
        byte[] utf8Bytes;
        String sFile = new String();
        // Reading input by lines:
        try {
            FileInputStream fis = new FileInputStream(fileName);
            int noOfBytes = fis.available();
            if (noOfBytes > 0) {
                utf8Bytes = new byte[noOfBytes];
                fis.read(utf8Bytes);
                sFile = new String(utf8Bytes, "UTF8");
            }
        } catch (Exception ex) {
            return null;
        }
        return sFile;
    }
    return null;
}

From source file:Main.java

/**
 * <pre>//from w  w  w. j  av  a2  s . c o  m
 * 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:Main.java

private static String loadBoard(File file) {
    String output = null;//from w w w  . j  av  a  2  s . c  o m
    try {
        FileInputStream is = new FileInputStream(file);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        output = new String(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return output;
}

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 av  a2s  .  c om
    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;
}