您的位置:首页 > 移动开发 > Android开发

Android创建指定大小、类型的文件

2015-11-10 10:41 363 查看
package com.testSDcardFill;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class CreateFile {

public enum FileUnit {
KB, MB, GB
}

/**
* 创建指定大小和类型的文件
* @author cxq
* @param targetFile 文件路径以及文件名,需要加后缀
* @param fileLength 文件大小
* @param unit 单位,KB,MB,GB
* @retrun boolean
*/
public boolean createFile(String targetFile, long fileLength, FileUnit unit) {
//指定每次分配的块大小
long KBSIZE = 1024;
long MBSIZE1 = 1024 * 1024;
long MBSIZE10 = 1024 * 1024 * 10;
switch (unit) {
case KB:
fileLength = fileLength * 1024;
break;
case MB:
fileLength = fileLength * 1024*1024;
break;
case GB:
fileLength = fileLength * 1024*1024*1024;
break;

default:
break;
}
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;
}
}


调用:

String filePath="/sdcard/A.txt";

int fileSize=10;

createFile(filePath, fileSize, FileUnit.MB);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: