Java構建指定大小文件


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM