一些小程序需要在Android
设备上跑一下,看看输出。写Android工程就太浪费精力了。
如下代码查看存储设备(路径/storage/external_storage/sda1/)剩余空间:
class="java">
import android.os.StatFs;
public class FreeDisk {
public static void main(String[] args) {
System.out.println("\nFreeMemory = " + FreeMemory("/storage/external_storage/sda1/"));
}
public static int FreeMemory(String path) {
StatFs statFs = new StatFs(path);
int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
long LongFree = ((long)statFs.getAvailableBlocks() * (long)statFs.getBlockSize()) / 1048576l;
double DoubleFree = ((double)statFs.getAvailableBlocks() * (double)statFs.getBlockSize()) / 1048576;
System.out.println("statFs.getAvailableBlocks() = " + statFs.getAvailableBlocks());
System.out.println("statFs.getBlockSize() = " + statFs.getBlockSize());
System.out.println("Free = " + Free);
System.out.println("LongFree = " + LongFree);
System.out.println("DoubleFree = " + DoubleFree);
return Free;
}
}
使用以下shell可以完成代码的执行:
#android.jar位于Android SDK下的platforms/android-xx目录下,把它配置在类库路径下
javac -classpath android.jar FreeDisk.java
#Android的dx工具,将classes文件编译为字节码dex文件
dx --dex --output=classes.dex FreeDisk.class
zip FreeDisk.zip classes.dex
adb push FreeDisk.zip /mnt/sdcard/
adb shell mkdir /mnt/sdcard/dalvik-cache
#在Android设备上执行代码
adb shell ANDROID_DATA=/mnt/sdcard dalvikvm -cp /mnt/sdcard/FreeDisk.zip FreeDisk
理论来说,int只有32位大小,只能够查看4GB
内存大小;而long有64位大小,能够查看16EB大小。所以一般的需要使用long来记录大小。
B 比特bit
1 kB = 1024 B (kB - kilobajt) 千
1 MB = 1024 kB (MB - megabajt) 兆
1 GB = 1024 MB (GB - gigabajt) 吉
1 TB = 1024 GB (TB - terabajt) 太
1 PB = 1024 TB (PB - petabajt) 拍
1 EB = 1024 PB (EB - eksabajt) 艾
1 ZB = 1024 EB (ZB - zettabajt) 皆
1 YB = 1024 ZB (YB - jottabajt) 佑
1 BB = 1024 JB (BB - brontobajt)