package com.example.testmemoryenouth;import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;import android.util.Log;public class FileUtil {
//这个最好public static String createEmptyFile(long filesize) {try {long vstart = System.currentTimeMillis();RandomAccessFile raf = new RandomAccessFile("/sdcard/temp", "rw");raf.setLength(filesize);raf.close();Log.e("SaveTime Costs: ", System.currentTimeMillis() - vstart+ " 毫秒.");return System.currentTimeMillis() - vstart + "";} catch (Exception e) {e.printStackTrace();}return "没有sd卡";}public static String createEmptyFile1(long filesize) {try {long vstart = System.currentTimeMillis();FileOutputStream fos = new FileOutputStream("/sdcard/temp");for (int i = 0; i < filesize / 1024 / 1024; i++) {byte[] buffer = new byte[1024 * 1024]; // 1次1Mfos.write(buffer);}Log.e("SaveTime Costs: ", System.currentTimeMillis() - vstart+ " 毫秒.");return System.currentTimeMillis() - vstart + "";} catch (Exception e) {e.printStackTrace();}return "没有sd卡";}public static String createEmptyFile3(long filesize) {try {long vstart = System.currentTimeMillis();File f = new File("/sdcard/temp");RandomAccessFile raf = new RandomAccessFile(f, "rw");raf.seek(filesize);raf.writeUTF("123");raf.close();Log.e("SaveTime Costs: ", System.currentTimeMillis() - vstart+ " 毫秒.");return System.currentTimeMillis() - vstart + "";} catch (Exception e) {e.printStackTrace();}return "没有sd卡";}// bigFile= open('test.dat', 'w')// bigFile.seek(1024*1024*1024* fileSize - 1) #fileSize = 2 代表生成2G// bigFile.write('\x00')// bigFile.close()
}
package com.example.testmemoryenouth;import java.io.File;import android.os.Environment;
import android.os.StatFs;
import android.util.Log;public class SDCardUtil {/*** * 取得空闲sd卡空间大小* * @return*/static public long getAvailaleSize() {File path = Environment.getExternalStorageDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long availableBlocks = stat.getAvailableBlocks();/* 返回bit大小值 */Log.e("The avaliable memory od the SDcard is: ", "" + availableBlocks * blockSize);return availableBlocks * blockSize;// (availableBlocks * blockSize)/1024 KB 单位// (availableBlocks * blockSize)/1024 /1024 MB单位}/*** * 获取SD卡总大小* * @return*/static public long getAllSize() {File path = Environment.getExternalStorageDirectory();StatFs stat = new StatFs(path.getPath());/* 获取block的SIZE */long blockSize = stat.getBlockSize();/* 块数量 */long availableBlocks = stat.getBlockCount();/* 返回bit大小值 */return availableBlocks * blockSize;}
}
package com.su.testjni;import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;public class CreateFileActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);long start = System.currentTimeMillis();createFile(100*1024*1024);long timecost = System.currentTimeMillis() - start;TextView tv = new TextView(this);tv.setText(timecost + "");setContentView(tv);}public native void createFile(long filesize);public native String unimplementedStringFromJNI();static {System.loadLibrary("createfile");}
}