Android 多线程2
16lz
2021-01-23
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
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
更多相关文章
- Android写文件到Sd卡的一般过程
- android 实现流媒体播放远程mp3文件代码
- android -- 小功能 Android为多媒体文件生成缩略图
- Android, 如何在C文件中加log
- Android文件夹大小
- Android中的多种文件读写操作方法