Java快速创建指定大小的文件,最多的解决办法就是循环向文件里面入固定大小的空字节,但是这种方式构建大文件性能比较低下,因此有这样两种方式可供参考:
Java有一个类:FileChannel,查阅API发现通过这个类来实现复制文件比简单的循环读取写入可能会高效得多,很多操作系统可将字节直接从文件系统缓存传输到目标通道,而无需实际复制各字节。构建大的文件10GB,20GB,150GB,所用时间都是100毫秒左右。
/**
* 创建固定大小的文件
* @param file
* @param length
* @throws IOException
*/
public static void createFixLengthFile(File file, long length) throws IOException{
long start = System.currentTimeMillis();
FileOutputStream fos = null;
FileChannel output = null;
try {
fos = new FileOutputStream(file);
output = fos.getChannel();
output.write(ByteBuffer.allocate(1), length-1);
} finally {
try {
if (output != null) {
output.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("total times "+(end-start));
}
另外一种方式就是RandomAccessFile类, 能够更方便,更直观的实现,两者效率相差无几,大文件RandomAccessFile大约是FileChannel的一倍,但是小文件RandomAccessFile效率就要高的多了,但这应该是更推荐的一种方式。
public static void create(File file, long length) throws IOException{
long start = System.currentTimeMillis();
RandomAccessFile r = null;
try {
r = new RandomAccessFile(file, "rw");
r.setLength(length);
} finally{
if (r != null) {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
System.out.println(end-start);
}
完整代码示例:

public class CreateFile { public static void main(String[] args) throws IOException { String filePath = "D:\\temp\\api-auto-test\\10000-files"; File file = new File(filePath); if(!file.exists()){ file.mkdirs(); } long start = System.currentTimeMillis(); for(int i=0;i<10000;i++){ String fileName = "auto-test1"+i; File f = new File(filePath,fileName); if(!f.exists()){ createFile(f,1l); } } long end = System.currentTimeMillis(); System.out.println("total times "+(end-start)); start = System.currentTimeMillis(); for(int i=0;i<10000;i++){ String fileName = "auto-test2"+i; File f = new File(filePath,fileName); if(!f.exists()){ createFixLengthFile(f,1l); } } end = System.currentTimeMillis(); System.out.println("total times "+(end-start)); } public static void createFixLengthFile(File file, long length) throws IOException{ FileOutputStream fos = null; FileChannel output = null; try { fos = new FileOutputStream(file); output = fos.getChannel(); output.write(ByteBuffer.allocate(1), length-1); } finally { try { if (output != null) { output.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } private static void createFile(File file, long length) throws IOException{ RandomAccessFile ff = null; try{ ff = new RandomAccessFile(file,"rw"); ff.setLength(length); }finally{ if (ff != null){ try{ ff.close(); }catch(Exception e){ e.printStackTrace(); } } } } }
Stay hungry,stay foolish !
分类:
Java基础
标签:
FileChannel
, RandomAccessFile
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构