工具类共享:随机SDcard填充

来源:互联网 发布:ardupilot3.3源码下载 编辑:程序博客网 时间:2024/06/05 03:05

实现Sdcard的随机填充,目录个数随机、文件个数随机、文件名随机、文件分布随机、文件类型随机,文件深度随机


在manifest文件添加权限

    <!-- 在SDCard中创建与删除文件权限 -->    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <!-- 往SDCard写入数据权限 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.util.Random;import java.util.regex.Matcher;import java.util.regex.Pattern;public class SDcardFillUtils {String splitChar = "/";private ProgressBarListener mListener;// 一级目录下的总文件夹个数,最多5-10个public int dir_count = (new Random()).nextInt(5) + 5;// 本次填充所有public int files_count = (new Random()).nextInt(5) + 5;// 文件夹和文件 的命名最大长度public int name_max_size = 20;// 单个文件最大sizepublic int singleFileMaxSize = 100;// 单个文件最小sizepublic int singleFileMinSize = 1;// 随机生成的文件类型public String[] fileTypes = { ".png", ".jpg", ".txt", ".zip", ".doc",".mp4" };// 随机生成路径的深度2-7int depth_count = (new Random()).nextInt(5) + 2;// 定义回调接口public interface ProgressBarListener {void onOperateFinished(int progress, int count);}public SDcardFillUntils(ProgressBarListener listener) {mListener = listener;}/** * 通过/的位置去截取路径* @param string 目标路径* @param pos "/"出现的位置,注意pos必须小于总的"/"的 个数*  */public static int getCharacterPosition(String string, int pos) {if (string.split("/").length < pos) {// 去掉最后一个“/”return string.length() - 1;}// 这里是获取"/"符号的位置Matcher slashMatcher = Pattern.compile("/").matcher(string);int mIdx = 0;while (slashMatcher.find()) {mIdx++;// 当"/"符号第三次出现的位置if (mIdx == pos) {break;}}return slashMatcher.start();}/** * 在指定的目录下随机生成文件和文件夹,文件名称、文件格式、文件夹名称、文件夹深度、文件所在路径均随机* @param rootDir 根路径* @param totalSize 所有文件加起来的总大小 */public void fillRandomFileAndDirs(String rootDir, long totalSize) {// 生成目录String[] dirsStrings = new String[dir_count];File[] dirFiles = new File[dir_count];for (int i = 0; i < dirsStrings.length; i++) {String dirPlus = "";for (int j = 0; j < depth_count; j++) {dirPlus += getRandomString(name_max_size) + "/";}dirsStrings[i] = rootDir + dirPlus;dirFiles[i] = new File(dirsStrings[i]);if (!dirFiles[i].exists()) {dirFiles[i].mkdirs();}}// 在已生成的路径中填充文件long remain_size = totalSize;int rootDepth = rootDir.split("/").length;android.util.Log.e("cxq", "dirs:" + dir_count);while (remain_size > 0) {// 在已创建的文件夹中获取路径int r = new Random().nextInt(dir_count);// 获取文件的深度String[] strs = dirsStrings[r].split("/");// 需要加上根路径的深度,不然文件的填充会跑到其他路径去int depth_r = new Random().nextInt(strs.length) + rootDepth;int index = getCharacterPosition(dirsStrings[r], depth_r);String newPath = dirsStrings[r].substring(0, index) + "/";android.util.Log.e("cxq", "s  " + newPath);// 在该路径下填充文件// 本次填充的文件大小,至少为1Mlong file_size = new Random().nextInt((int) remain_size) + 1;long used=0L;while (file_size > singleFileMaxSize|| file_size < singleFileMinSize) {file_size = new Random().nextInt(dir_count);}remain_size = remain_size - file_size;android.util.Log.e("cxq", "total:" + totalSize + " this:"+ file_size + " remain:" + remain_size);String newFileName = newPath + getRandomString(name_max_size)+ fileTypes[new Random().nextInt(fileTypes.length)];createFile(newFileName, file_size);// 用于进度条的更新if (mListener != null) {used=totalSize-remain_size;android.util.Log.e("cxq", " "+used+" "+totalSize);mListener.onOperateFinished((int)used, (int)totalSize);}}}/** * 生成随机的最大长度为maxlength的字符串,包括数字和字母 * @param maxLength 最大的随机长度 *  */public static String getRandomString(int maxLength) {// ACII对应关系0-9[48-57]// A-Z[65-90],a-z[97-122]// 为防止随机生成的长度为0,造成获得的String为空,需要默认加1int length = new Random().nextInt(maxLength) + 1;char[] charArray = new char[length];for (int i = 0; i < length; i++) {Random r = new Random();int n = r.nextInt(123);while (n < 48 || (n > 57 && n < 65) || (n > 90 && n < 97)|| n > 122) {n = r.nextInt(123);}charArray[i] = (char) n;}return String.valueOf(charArray);}public boolean createFile(String targetFile, long fileLength) {long KBSIZE = 1024;long MBSIZE1 = 1024 * 1024;long MBSIZE10 = 1024 * 1024 * 10;fileLength = fileLength * 1048576;FileOutputStream fos = null;File file = new File(targetFile);try {if (!file.exists()) {file.createNewFile();}long batchSize = 0;batchSize = fileLength;if (fileLength > KBSIZE) {batchSize = KBSIZE;}if (fileLength > MBSIZE1) {batchSize = MBSIZE1;}if (fileLength > MBSIZE10) {batchSize = MBSIZE10;}long count = fileLength / batchSize;long last = fileLength % batchSize;fos = new FileOutputStream(file);FileChannel fileChannel = fos.getChannel();for (int i = 0; i < count; i++) {ByteBuffer buffer = ByteBuffer.allocate((int) batchSize);fileChannel.write(buffer);}if (last != 0) {ByteBuffer buffer = ByteBuffer.allocate((int) last);fileChannel.write(buffer);}fos.close();return true;} catch (IOException e) {e.printStackTrace();} finally {try {if (fos != null) {fos.close();}} catch (IOException e) {e.printStackTrace();}}return false;}}

Activity中的调用

// 专门用于更新进度条(接口回调)随机填充private class RandomListener implementsSDcardFillUtils.ProgressBarListener {@Overridepublic void onOperateFinished(int progress, int count) {publishProgress((progress + 1) * 100 / count);}}

RandomListener rls = new RandomListener();SDcardFillUntils sDcardFillUtils = new SDcardFillUtils(rls);
File sdcardDir = Environment.getExternalStorageDirectory();// 得到一个路径,内容是sdcard的文件夹路径和名字String path = sdcardDir.getPath() +  "/A/";sDcardFillUntils.fillRandomFileAndDirs(path, fillSiz);


0 0
原创粉丝点击