随笔 - 361,  文章 - 0,  评论 - 62,  阅读 - 160万

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();
                }
            }
        }
    } 
}
View Code
复制代码
posted on   kosamino  阅读(5581)  评论(3编辑  收藏  举报
编辑推荐:
· .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语句:使用策略模式优化代码结构

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示