Android 多线程2

public class MyThread extends Thread {

private Handler hd1;

public MyThread(Handler handler) {
this.hd1 = handler;
}

public void run() {
try {
copyFile(new File("/sdcard/fill.file"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}// run()

    public static void copyFile(File targetFile)  throws IOException
    {       
        // 新建文件输出流并对它进行缓冲
    targetFile.delete();
        FileOutputStream output = new FileOutputStream(targetFile);
        BufferedOutputStream outBuff=new BufferedOutputStream(output);
       
        // 缓冲数组
        byte[] b = new byte[1024 * 1024 * 8];
        while (getSD()>((long)8)) {
            outBuff.write(b, 0, 1024 * 1024 * ;
        }
        // 刷新此缓冲的输出流
        outBuff.flush();
        
        //关闭流
        outBuff.close();
        output.close();
    }

public static long getSD() {
String sDcString = android.os.Environment.getExternalStorageState();
if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {
// 取得sdcard文件路径
File pathFile = android.os.Environment
.getExternalStorageDirectory();
android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
// 获取SDCard上BLOCK总数
long nTotalBlocks = statfs.getBlockCount();
// 获取SDCard上每个block的SIZE
long nBlocSize = statfs.getBlockSize();
// 获取可供程序使用的Block的数量
long nAvailaBlock = statfs.getAvailableBlocks();
// 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)
long nFreeBlock = statfs.getFreeBlocks();
// 计算SDCard 总容量大小MB
long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
// 计算 SDCard 剩余大小MB
long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;

return nSDFreeSize;
}// if
else {
return -100;
}
}

}// class

你可能感兴趣的:(thread,多线程,android,OS)