2012-06-02 23 views
7

我有一個奇怪的問題要問。我需要知道如何使用Java(任何文件類型)將給定的文件分解成小的小塊。然後,我可以將這些茶葉放入CD的數量中,然後帶走。我嘗試過使用this的方式如何使用Java將文件分解爲碎片?

但是,正如大多數用戶所評論的,我所嘗試的是不可能以這種方式實現的。所以,我決定提出一個新的問題來獲得一個破解文件的正確方法。

當我將一個文件分解成幾部分(猜測30個)時,必須有一種方法將它們重新組裝回來並創建原始文件。請幫忙。

回答

25

當緩衝區已滿或文件結束時,將字節的部分讀取到字節數組中,並將它們存儲在新文件中。

例如(代碼是不完美的,但它應該幫助理解的過程)

class FileSplit { 
    public static void splitFile(File f) throws IOException { 
     int partCounter = 1;//I like to name parts from 001, 002, 003, ... 
          //you can change it to 0 if you want 000, 001, ... 

     int sizeOfFiles = 1024 * 1024;// 1MB 
     byte[] buffer = new byte[sizeOfFiles]; 

     String fileName = f.getName(); 

     //try-with-resources to ensure closing stream 
     try (FileInputStream fis = new FileInputStream(f); 
      BufferedInputStream bis = new BufferedInputStream(fis)) { 

      int bytesAmount = 0; 
      while ((bytesAmount = bis.read(buffer)) > 0) { 
       //write each chunk of data into separate file with different number in name 
       String filePartName = String.format("%s.%03d", fileName, partCounter++); 
       File newFile = new File(f.getParent(), filePartName); 
       try (FileOutputStream out = new FileOutputStream(newFile)) { 
        out.write(buffer, 0, bytesAmount); 
       } 
      } 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     splitFile(new File("D:\\destination\\myFile.mp4")); 
    } 
} 

myFile.mp4大小= 12.7 MB

拆分後,我有13個文件

  • myFile.mp4.001 - myFile.mp4.012大小1 MB
  • myFile.mp4.013大小806 KB

如果要合併這些文件,你可以使用

public static void mergeFiles(List<File> files, File into) 
     throws IOException { 
    try (FileOutputStream fos = new FileOutputStream(into); 
     BufferedOutputStream mergingStream = new BufferedOutputStream(fos)) { 
     for (File f : files) { 
      Files.copy(f.toPath(), mergingStream); 
     } 
    } 
} 

你也可以創造一些額外的方法,使您的生活更輕鬆。例如,將根據這些文件之一的名稱(和位置)創建包含分離部分的文件列表的方法。

public static List<File> listOfFilesToMerge(File oneOfFiles) { 
    String tmpName = oneOfFiles.getName();//{name}.{number} 
    String destFileName = tmpName.substring(0, tmpName.lastIndexOf('.'));//remove .{number} 
    File[] files = oneOfFiles.getParentFile().listFiles(
      (File dir, String name) -> name.matches(destFileName + "[.]\\d+")); 
    Arrays.sort(files);//ensuring order 001, 002, ..., 010, ... 
    return Arrays.asList(files); 
} 

根據該方法,我們可以重載mergeFiles方法只使用這些文件的File oneOfFiles,而不是整個列表List<File>之一(我們將基於文件的一個列表)

public static void mergeFiles(File oneOfFiles, File into) 
     throws IOException { 
    mergeFiles(listOfFilesToMerge(oneOfFiles), into); 
} 

你可以也重載這些方法使用String代替File(在需要的時候,我們將每個包裹中的字符串文件)

public static List<File> listOfFilesToMerge(String oneOfFiles) { 
    return listOfFilesToMerge(new File(oneOfFiles)); 
} 

public static void mergeFiles(String oneOfFiles, String into) throws IOException{ 
    mergeFiles(new File(oneOfFiles), new File(into)); 
} 
+0

太好了。非常感謝:) –

+0

那麼合併? –

+0

@ldce我更新了我的答案。希望你會喜歡。 – Pshemo

3

閱讀&將原始碼寫入byte[]。避免文本流和讀者。

在你最後一個問題中,你顯然是根據'行'分解文件。要複製該行爲,只需使用固定大小的byte[]即可閱讀。請仔細留意最後一個問題的註釋中的警告,以檢查讀取的字節數。一個byte[2^16]不一定會填入一個單一的閱讀。

+0

尊敬安德魯,我幾乎厭倦了這一點。如果你尋求我的問題,你可能會發現另一個問題,我問了幾個月前類似的問題。那麼,請你給我一個示例代碼,讓他足夠友善一點?這將不勝感激:) –