Back to project page filemanager.
The source code is released under:
MIT License
If you think the Android project filemanager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.binkery.app.filemanager.utils; /*from ww w .ja v a2 s . co m*/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import android.annotation.SuppressLint; import android.os.Build; import android.os.StatFs; public class FileUtils { private static final String TAG = FileUtils.class.getSimpleName(); public static boolean delete(File file) { if (file == null) { return false; } if (!file.exists()) { return false; } File[] files = file.listFiles(); if (files != null && files.length != 0) { for (File f : files) { if (!delete(f)) { return false; } } } return file.delete(); } public static boolean delete(String path){ File file = new File(path); return delete(file); } public static long getFreeSpace(File file) { if (file == null || !file.exists() || file.isFile()) { return 0; } StatFs sf = new StatFs(file.getAbsolutePath()); if (Build.VERSION.SDK_INT > 18) { return getFressSpcaceHeight(sf); } return getFressSpcaceLow(sf); } public static long getFileSize(File file) { if (file == null || !file.exists()) { return 0; } if (file.isFile()) { return file.length(); } File[] files = file.listFiles(); if (files == null || files.length == 0) { return 0; } long filesize = 0; for (File f : files) { filesize += getFileSize(f); } return filesize; } private static final int MIN_BUFFER_SIZE = 50 * 1024; private static final int MAX_BUFFER_SIZE = 1024 * 1024; private static int getBufferSize(File file) { if (file == null || !file.exists()) { return 0; } long fileSize = file.length(); if (fileSize > MAX_BUFFER_SIZE) { return MAX_BUFFER_SIZE; } if (fileSize < MIN_BUFFER_SIZE) { return (int) fileSize; } return MIN_BUFFER_SIZE; } /** * Copy a file from 'from' file to 'to' file. * * @param from * @param to * @return return the file size if successful and -1 if failed . It maybe * failed if the from file not exist , or the to file is exist and * can not delete , or something others . */ public static long copy(File from, File to) { if (from == null || !from.exists()) { return -1; } if (to != null && to.exists() && !to.delete()) { return -1; } InputStream is = null; OutputStream os = null; try { is = new FileInputStream(from); os = new FileOutputStream(to); int bufferSize = getBufferSize(from); byte buffer[] = new byte[bufferSize]; int length; while ((length = is.read(buffer)) != -1) { Logs.i(TAG, "length" + length); os.write(buffer, 0, length); os.flush(); } return 1; } catch (Exception ex) { return -1; } finally { if (is != null) { try { is.close(); } catch (Exception e) { } try { os.close(); } catch (Exception e) { } } } } @SuppressLint("NewApi") private static long getFressSpcaceHeight(StatFs sf) { long blockSize = sf.getBlockSizeLong(); long availCount = sf.getAvailableBlocksLong(); return blockSize * availCount; } @SuppressWarnings("deprecation") private static long getFressSpcaceLow(StatFs sf) { long blockSize = sf.getBlockSize(); long availCount = sf.getAvailableBlocks(); return blockSize * availCount; } }