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 boolean equalSize(String path1, String path2) {
    File srcF = new File(path1);
    File srcF2 = new File(path2);
    if (srcF.length() == srcF2.length())
        return true;
    else//from   w w  w. j  av  a2s.  c  om
        return false;
}

From source file:Main.java

public static boolean isFileExistAndNotNull(String filePath) {
    if (filePath == null || filePath.length() == 0) {
        return false;
    }/*from   www. jav a  2s  . c  o  m*/
    File file = new File(filePath);
    if (file != null && file.exists() && file.length() > 0) {
        return true;
    }
    return false;
}

From source file:Main.java

public static byte[] readFileToByteArray(File file) {
    try {/*from  ww  w .  ja v a2s. c  o m*/
        byte[] fileData = new byte[(int) file.length()];
        DataInputStream dis = new DataInputStream(new FileInputStream(file));
        dis.readFully(fileData);
        dis.close();
        return fileData;
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static boolean fileAvaliable(String path) {
    boolean flag = false;
    if (!TextUtils.isEmpty(path)) {
        File file = new File(path);
        flag = file.exists() && file.isFile() && file.length() > 0;
    }//w  ww .  j a v  a2 s .c o m
    return flag;
}

From source file:Main.java

public static String readTxtFromFile(File file) throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
    FileInputStream in = new FileInputStream(file);
    in.getChannel().read(buffer);// w  w w. j av  a  2  s  . c om
    in.close();
    return new String(buffer.array());
}

From source file:Main.java

private static long getDirSize(File dir) {
    long size = 0;
    File[] files = dir.listFiles();

    for (File file : files) {
        if (file.isFile()) {
            size += file.length();
        }/*from  w w w.  ja  va2 s .c  o  m*/
    }

    return size;
}

From source file:Main.java

private static long getFileSize(String path) {
    if (TextUtils.isEmpty(path)) {
        return 0;
    }//ww w.  j  a v  a 2s .  co m
    File filePath = new File(path);
    File[] itemList = filePath.listFiles();
    long totalSize = 0;
    if (null != itemList) {
        for (File f : itemList) {
            if (f.exists() && f.isFile() && f.length() > 0) {
                totalSize = totalSize + f.length();
            }
        }
    }
    return totalSize;
}

From source file:Main.java

public static boolean validImage(String path, String url, int res) {
    if (0 != res) {
        return true;
    } else if (null != path && path.length() != 0 && path.length() < KB_10) {
        final File file = new File(path);
        if (!(file.exists() && file.length() < MB_5)) {
            if (null != url && url.length() != 0) {
                return true;
            }/*w ww  . java2 s . c o  m*/
        }
        return true;
    } else if (null != url && url.length() != 0) {
        return true;
    }
    return false;
}

From source file:Main.java

/**
 * Get give package apk file size.//from   w ww.  jav a 2 s  . c om
 * 
 * @param context Object of {@link Context}.
 * @param pkg Package name.
 * @return Apk file size by bytes.
 */
public final static long getAppSize(Context context, String pkg) {
    if (null == context || null == pkg) {
        return 0;
    }

    PackageManager pm = context.getPackageManager();
    try {
        ApplicationInfo info = pm.getApplicationInfo(pkg, 0);
        File file = new File(info.sourceDir);
        return file.length();

    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

static byte[] read(String fname) throws Exception {
    long offset = 0;
    File f = new File(fname);
    long length = f.length();
    byte[] image = new byte[(int) length];
    FileInputStream fis = new FileInputStream(f);
    while (offset < length) {
        offset += fis.read(image, (int) offset, (int) (length - offset));
    }/*from w  w w .j av  a2 s .c  o  m*/
    fis.close();
    return image;
}