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 long getFileSize(String path) {
    File file = new File(path);
    if (!file.exists()) {
        return -1;
    } else {//  w w w .  j av  a  2 s  .c om
        return file.length();
    }
}

From source file:Main.java

public static long getFileSize(String fn) {
    File f = null;
    long size = 0;

    try {//from  w  w  w. j  ava  2  s .c  o  m
        f = new File(fn);
        size = f.length();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        f = null;
    }
    return size < 0 ? null : size;
}

From source file:Main.java

public static long folderSize(File directory, boolean rootMode) {
    long length = 0;
    for (File file : directory.listFiles()) {

        if (file.isFile())
            length += file.length();
        else/*from w  w w .  j  a va  2s .  c  om*/
            length += folderSize(file, rootMode);
    }
    return length;
}

From source file:Main.java

public static String loadString(String filename) throws Exception {
    File file = new File(sAppContext.getCacheDir().getPath() + "/" + filename);
    byte[] buff = new byte[(int) file.length()];
    FileInputStream fis = new FileInputStream(file);
    fis.read(buff);//w w w. j a va 2  s.c  om
    String string = new String(buff);
    return string;
}

From source file:Main.java

public static long getLength(String path) {
    File file = new File(path);
    if (!file.isFile() || !file.exists()) {
        return 0;
    }//  w ww  .j  a v a 2s .c  o  m
    return file.length();
}

From source file:Main.java

private static int decodeFileLength(String filePath) {
    if (TextUtils.isEmpty(filePath)) {
        return 0;
    }//from w  w  w .  j  a va  2s.  c o m
    File file = new File(filePath);
    if (!file.exists()) {
        return 0;
    }
    return (int) file.length();
}

From source file:org.dd4t.core.util.IOUtils.java

public static String convertFileToString(String filePath) {
    FileSystemResource resource = new FileSystemResource(filePath);
    File file = resource.getFile();
    byte[] buffer = new byte[(int) file.length()];
    try {//  w ww  . ja  v a 2 s. c  o  m
        resource.getInputStream().read(buffer);
        resource.getInputStream().close();
        return new String(buffer);
    } catch (IOException e) {
        return null;
    }
}

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;
    }/* ww w. j a v a2s.  c  o  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

public static long getFileSize(String path) {
    if (TextUtils.isEmpty(path)) {
        return -1;
    }//from   w w  w.  ja  v  a  2s. c om

    File file = new File(path);
    return (file.exists() && file.isFile() ? file.length() : -1);
}

From source file:id.co.nlp.MachineTranslation.Utils.Util.java

public static String read(String path) throws FileNotFoundException, IOException {
    File file = new File(path);
    long l = file.length();
    byte[] b = new byte[(int) l];
    FileInputStream fileInputStream = new FileInputStream(file);
    fileInputStream.read(b);/*from w  w  w.  j  a  va 2s .  c om*/
    return new String(b);
}