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 2018-06-13 20:47  kosamino  阅读(5435)  评论(3编辑  收藏  举报