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:com.github.drbookings.io.Backup.java

public static void make(final File file) {
    if (file.exists() && file.length() != 0) {
        try {/*from  w w  w  . j  a  v a  2  s. c  om*/
            final File backupFile = new File(file.getParentFile(), file.getName() + ".bak");
            FileUtils.copyFile(file, backupFile);
            if (logger.isInfoEnabled()) {
                logger.info("Backup created as " + backupFile);
            }
        } catch (final IOException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getLocalizedMessage(), e);
            }
        }
    }
}

From source file:Main.java

public static String read(File src) throws IOException {
    long length = src.length();

    // Probably not the best way to do it, but I'm lazy.
    char[] chars = new char[(int) length];
    FileReader fileReader = new FileReader(src);
    fileReader.read(chars);//from w w w .  j  av a 2 s. c  o m
    fileReader.close();
    return new String(chars);
}

From source file:Main.java

public static long getFileSize(String fileName) {

    long fileSize = 0L;

    File file = new File(fileName);
    if (file.exists()) {
        fileSize = file.length();

    }/*  w  ww  . j a  va  2  s  . c om*/
    return fileSize;
}

From source file:Main.java

public static byte[] read(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    InputStream ios = null;/*ww w  . ja  v a  2 s.co  m*/
    try {
        ios = new FileInputStream(file);
        if (ios.read(buffer) == -1)
            throw new IOException("EOF reached while trying to read the whole file");
    } finally {
        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }
    return buffer;
}

From source file:Main.java

public static long folderSize(File directory) {
    long length = 0;
    File[] files = directory.listFiles();
    if (files != null)
        for (File file : files)
            if (file.isFile())
                length += file.length();
            else//from   ww w .  ja  va 2s .  c  o  m
                length += folderSize(file);
    return length;
}

From source file:Main.java

public static String getETag(File file) {
    return Integer.toHexString((file.getAbsolutePath() + file.lastModified() + "" + file.length()).hashCode());
}

From source file:Main.java

public static boolean installApk(Context context, String filePath) {
    File file = new File(filePath);
    if (!file.exists() || !file.isFile() || file.length() <= 0) {
        return false;
    }//ww w .  ja  v a2  s . com
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
    return true;
}

From source file:Main.java

public static byte[] readFile(File file) throws IOException {
    int len = (int) file.length();
    if (len == 0) {
        return new byte[0];
    }// ww w .  j av a2  s .co  m

    byte[] data = null;
    BufferedInputStream bis = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        data = new byte[len];
        bis.read(data);
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
            }
        }
    }

    return data;
}

From source file:Main.java

private static long getDirSize(File dir) {

    long size = 0;
    if (!dir.exists()) {
        dir.mkdir();/*from  www . j  a  va 2s. co m*/
    }
    File[] files = dir.listFiles();

    for (File file : files) {
        if (file.isFile()) {
            size += file.length();
        }
    }

    return size;
}

From source file:Main.java

public static byte[] getByteArray(File filePath, String name) {
    FileInputStream fileInputStream = null;

    File file = new File(filePath, name);

    byte[] bFile = new byte[(int) file.length()];

    try {/*from w  ww . ja  v a  2s.  c o  m*/
        //convert file into array of bytes
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bFile;
}