Example usage for java.io File length

List of usage examples for java.io File length

Introduction

In this page you can find the example usage for java.io File length.

Prototype

public long length() 

Source Link

Document

Returns the length of the file denoted by this abstract pathname.

Usage

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);/*w w w .  j a va2s.c  om*/
    insr.close();
    return new String(fileBuffer);
}

From source file:Main.java

/**
 0: number of files//  ww  w. j a va 2  s .  c  o  m
 1: total length
 2: number of directories
 */
public static void getDirSize(final File f, final long[] l) {
    if (f.isFile()) {
        l[0]++;
        l[1] += f.length();
    } else {
        Stack<File> stk = new Stack<>();
        stk.add(f);
        File fi = null;
        File[] fs;
        while (stk.size() > 0) {
            fi = stk.pop();
            fs = fi.listFiles();
            for (File f2 : fs) {
                if (f2.isDirectory()) {
                    stk.push(f2);
                    l[2]++;
                } else {
                    l[0]++;
                    l[1] += f2.length();
                }
            }
        }
    }
}

From source file:com.cloud.servlet.StaticResourceServlet.java

static String getEtag(final File resource) {
    return "W/\"" + resource.length() + "-" + resource.lastModified();
}

From source file:Main.java

public static String readFromFile(File file) {
    try (Reader in = new InputStreamReader(new FileInputStream(file), "UTF-8")) {
        char[] arr = new char[(int) file.length()];
        int len = in.read(arr);
        return new String(arr, 0, len);
    } catch (IOException ex) {
        throw new RuntimeException("Unable to read file " + file.getName(), ex);
    }/*from  w w w  . ja  va2 s.  com*/
}

From source file:Main.java

public static long getFileSize(String filename) {
    File file = new File(filename);

    if (!file.exists() || !file.isFile()) {
        System.out.println("File doesn\'t exist");
        return -1;
    }//from ww  w . j a v  a  2s  .co m
    return file.length();
}

From source file:librarymanagementsystem.Base64Converter.java

/**
 * This method loads a file from file system and returns the byte array of
 * the content./*from   www .ja v  a 2s .  co m*/
 */
public static byte[] loadFileAsBytesArray(String fileName) throws Exception {

    File file = new File(fileName);
    int length = (int) file.length();
    BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
    byte[] bytes = new byte[length];
    reader.read(bytes, 0, length);
    reader.close();
    return bytes;

}

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  ww .  j  a  v a 2s . com*/
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 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);/*www .  ja  v a 2  s  .co m*/
    return new String(buf);
}

From source file:Main.java

public static Bitmap decode(File file, int targetWidth, int targetHeight) {

    if (file == null || !file.exists() || file.length() == 0) {

        return null;
    }/*from   www  . j  av a 2 s.  co m*/

    String pathName = file.getPath();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);

    /*
     * If set to a value > 1, requests the decoder to subsample the original
     * image, returning a smaller image to save memory. The sample size is
     * the number of pixels in either dimension that correspond to a single
     * pixel in the decoded bitmap. For example, inSampleSize == 4 returns
     * an image that is 1/4 the width/height of the original, and 1/16 the
     * number of pixels. Any value <= 1 is treated the same as 1. Note: the
     * decoder uses a final value based on powers of 2, any other value will
     * be rounded down to the nearest power of 2.
     */
    options.inSampleSize = computeImageSampleSize(options, targetWidth, targetHeight);

    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(pathName, options);

}

From source file:Main.java

public static String readFromFile(String path) {
    String str = null;//from   w  w  w  . j  a  va 2 s .  c o  m
    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;
}