2010-01-21 83 views
0

有沒有什麼方法可以在5 MB的10個文件中存儲大的二進制文件,如50 MB? 謝謝 有沒有特殊的課程?存儲一個大的二進制文件

+4

我不知道這個機器人是否可以禁止 – stacker 2010-01-21 21:10:02

+0

所以,你有50個MB的文件你想分成10個5 MB的文件,並且你想用Java來完成它? – John 2010-01-21 21:10:53

+1

堆垛機,好奇你爲什麼懷疑機器人......很多沒有答案的問題? – John 2010-01-21 21:13:04

回答

0

是的,有。基本上只計算你寫入文件的字節數,如果它達到一定的限制,然後停止寫入,重置計數器並繼續使用某個文件名模式寫入另一個文件,這樣就可以使文件相互關聯。你可以做一個循環。你可以學習here如何寫入Java文件和補餘隻是應用小學數學。

1

使用FileInputStream讀取文件和FileOutputStream來寫入它。
這裏一個簡單的(不完全)的例子(缺少錯誤處理,寫入1K塊)

public static int split(File file, String name, int size) throws IOException { 
    FileInputStream input = new FileInputStream(file); 
    FileOutputStream output = null; 
    byte[] buffer = new byte[1024]; 
    int count = 0; 
    boolean done = false; 
    while (!done) { 
     output = new FileOutputStream(String.format(name, count)); 
     count += 1; 
     for (int written = 0; written < size;) { 
     int len = input.read(buffer); 
     if (len == -1) { 
      done = true; 
      break; 
     } 
     output.write(buffer, 0, len); 
     written += len; 
     } 
     output.close(); 
    } 
    input.close(); 
    return count; 
    } 

,並呼籲像

File input = new File("C:/data/in.gz"); 
String name = "C:/data/in.gz.part%02d"; // %02d will be replaced by segment number 
split(input, name, 5000 * 1024)); 
相關問題