2011-09-28 28 views
3

我有兩個AFP文件,我想將它們連接在一起,我該如何實現這一點。我已經編寫了Java代碼來連接它們,使用BufferedInputStream和BufferedOutputStream,結果AFP格式不正確。我甚至嘗試使用Linux貓,但產生相同的錯誤結果。請幫忙。我不認爲這個問題是我的Java代碼,但我發佈了下面的代碼以防萬一。如何將兩個AFP文件連接在一起

注意:一個奇怪的是,如果我切換串聯的順序,那麼它會產生正確的格式輸出。例如,如果我將A.afp和B.afp連接起來,那麼輸出就會混亂起來,但是如果我連接B.afp,然後A.afp,那麼它會產生正確的格式結果。但我需要A.afp出現B.afp

public static void main(String[] args) { 
    String filePath1 = "C:\\dev\\harry\\ETCC_data\\3199_FI_20_20110901143009.afp"; 
    String filePath2 = "C:\\dev\\harry\\ETCC_data\\3643_FI_49_20110901143006.afp"; 

    ConcatenateMain cm = new ConcatenateMain(); 
    cm.concate(filePath1, filePath2); 
} 

private void concate(String filePath1, String filePath2){ 
    BufferedInputStream bis1 = null; 
    BufferedInputStream bis2 = null; 
    FileInputStream inputStream1 = null; 
    FileInputStream inputStream2 = null; 
    FileOutputStream outputStream = null; 
    BufferedOutputStream output = null; 
    try{ 
     inputStream1 = new FileInputStream(filePath1); 
     inputStream2 = new FileInputStream(filePath2); 
     bis1 = new BufferedInputStream(inputStream1); 
     bis2 = new BufferedInputStream(inputStream2); 
     List<BufferedInputStream> inputStreams = new ArrayList<BufferedInputStream>(); 
     inputStreams.add(bis1); 
     inputStreams.add(bis2); 
     outputStream = new FileOutputStream("C:\\dev\\harry\\ETCC_data\\output.afp"); 
     output = new BufferedOutputStream(outputStream); 
     byte [] buffer = new byte[BUFFER_SIZE]; 
     for(BufferedInputStream input : inputStreams){ 
      try{ 
       int bytesRead = 0; 
       while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1) 
       { 
        output.write(buffer, 0, bytesRead); 
       } 
      }finally{ 
       input.close(); 
      } 
     } 
    }catch(IOException e){ 

    }finally{ 
     try { 
      output.close(); 
     } catch (IOException e) { 

     } 
    } 
} 

回答

2

AFP包含內聯資源在頁面的頂部,這樣

AFP file 1 resources  AND  AFP file 2 resources 
AFP file 1 content     AFP file 2 content 

然而,當我試圖來連接這兩個文件一起,一些資源將在連結文件的中間,因此陷入困境的結果

AFP file 1 resources 
AFP file 1 content 
AFP file 2 resources ------> resources should not be in the middle page 
AFP file 2 content 

因此該解決方案是將所有的資源出口一個外部文件,那麼你可以連接如下

AFP file resources 
AFP file 1 content 
AFP file 2 content 

這將解決這個問題。

0

之前,例如油庫,這裏有一個快速的函數來獲取從一個文件中的字節:

public static byte[] getBytesFromFile(File file) throws IOException { 
    InputStream is = new FileInputStream(file); 

    // Get the size of the file 
    long length = file.length(); 

    // Create the byte array to hold the data 
    byte[] bytes = new byte[(int)length]; 

    // Read in the bytes 
    int offset = 0; 
    int numRead = 0; 
    while (offset < bytes.length 
      && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
     offset += numRead; 
    } 

    // Ensure all the bytes have been read in 
    if (offset < bytes.length) { 
     throw new IOException("Could not completely read file "+file.getName()); 
    } 

    // Close the input stream and return bytes 
    is.close(); 
    return bytes; 
} 

然後,只需加載它們都寫它在默認情況下通過的Xenos D2E軟件生成的文件

try { 
    FileOutputStream fos = new FileOutputStream("C:\\dev\\harry\\ETCC_data\\output.afp"); 
    byte[] bytes1 = getBytesFromFile(new File(filePath1)); 
    byte[] bytes2 = getBytesFromFile(new File(filePath2)); 
    fos.write(bytes1); 
    fos.write(bytes2); 
    fos.flush(); 
    fos.close(); 
} 
catch(FileNotFoundException ex) { System.out.println("FileNotFoundException : " + ex); } 
catch(IOException ioe) { System.out.println("IOException : " + ioe); } 
+0

如果你看看我的代碼,它會在'byte []'中讀取,並寫出'byte []',這正是你的代碼正在做的事情。不過謝謝。 –

0

爲了搭載上面有關重新排序文件內容的答案,這裏是我在DST輸出(一個非常大的打印&郵件公司)工作時建立的一個建議工具。

我製作了一個名爲「afp_dd」的實用程序,它的工作方式與unix「dd」命令類似,允許我在命令行上指定記錄跳過和計數值,以提取打破記錄邊界的子文件(標準dd程序需要固定大小的記錄,而不是每個記錄開始附近的長度指示符的可變大小)。我可以通過我們的AFP轉儲程序管理子文件來檢查它們,然後使用輸出子文件作爲輸入來創建更改的文件。

相關問題