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 checkFile(File f) {
    if (f != null && f.exists() && f.canRead() && (f.isDirectory() || (f.isFile() && f.length() > 0))) {
        return true;
    }//from  ww w .j  ava2  s. c o m
    return false;
}

From source file:Main.java

public static long getFileSize(String filepath) {
    if (TextUtils.isEmpty(filepath)) {
        return -1;
    }/*from w  ww.  ja  v  a2  s  . co m*/
    File file = new File(filepath);
    return (file.exists() && file.isFile() ? file.length() : -1);
}

From source file:Main.java

public static String scaleImageFile(String filepath, int targetSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    File file = new File(filepath);
    float scaleFactor = file.length() / targetSize;
    options.inSampleSize = Math.round(scaleFactor);
    Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);

    int dotPos = filepath.lastIndexOf('.');
    String newFilePath = String.format("%s_scaled.%s", filepath.substring(0, dotPos),
            filepath.substring(dotPos + 1, filepath.length()));
    FileOutputStream fos = null;//from  ww w .  j  a v a 2  s. co  m
    try {
        fos = new FileOutputStream(newFilePath);
        if (!bitmap.compress(CompressFormat.JPEG, 90, fos)) {
            newFilePath = null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        newFilePath = null;
    }
    bitmap.recycle();
    return newFilePath;
}

From source file:Main.java

static public String readFile(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    DataInputStream input = null;
    try {/*from w  ww .  j a v  a 2s .  co  m*/
        input = new DataInputStream(new FileInputStream(file));
        input.readFully(buffer);
    } finally {
        closeQuietly(input);
    }
    return new String(buffer);
}

From source file:Main.java

/**
 * Calculates the size of the given file located in internal storage.
 *
 * @author Ian Copland//from   ww  w .  j  a v a2  s. c  o m
 *
 * @param in_context - The active context.
 * @param in_filePath - The path to the file which is to have its size calculated.
 *
 * @return The size of the file in bytes.
 */
public static long getFileSize(Context in_context, String in_filePath) {
    File file = in_context.getFileStreamPath(in_filePath);
    return file.length();
}

From source file:Main.java

public static boolean installApp(Context context, String filePath) {
    if (filePath != null && filePath.length() > 4
            && filePath.toLowerCase().substring(filePath.length() - 4).equals(".apk")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        File file = new File(filePath);
        if (file.exists() && file.isFile() && file.length() > 0) {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            return true;
        }//from ww w  .j  a va  2s.co m
    }
    return false;
}

From source file:Main.java

/**
 * Return file size from Uri/*from  w w  w.  j  a  v a  2  s.c o  m*/
 *
 * @param uri file URI
 * @return return file size
 */
public static long getFileSizeFromUri(Context context, Uri uri) {
    long size = 0;
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
                size = cursor.getInt(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        size = file.length();
    }
    return size;
}

From source file:Main.java

public static long getFileSize(Context context, String filePath) {
    if (context != null && !TextUtils.isEmpty(filePath)) {
        File f = new File(filePath);
        if (f.exists() && f.isFile()) {
            return f.length();
        }//w w w. j  a  v a 2s . c  o m
    }
    return -1l;
}

From source file:Main.java

private static void delete(File file, StringBuilder sb) {
    long length = file.length();
    boolean deleted = file.delete();
    if (deleted) {
        sb.append(file.getAbsolutePath() + " length " + length + " bytes, deleted.\r\n");
    } else {/*from   ww  w .j  ava2  s.  c  om*/
        sb.append(file.getAbsolutePath() + " length " + length + " bytes, can't delete.\r\n");
    }
}

From source file:Main.java

public static long getFileSize(File pFile) {
    long size = 0;

    if (pFile != null && pFile.exists()) {
        size = pFile.length();
    }/*from   w  ww.j a v  a 2 s  .  c  om*/
    return size;
}