2012-06-26 61 views
3

我有這段代碼可以將文件從IFS複製到本地驅動器。我想問一些關於如何改善它的建議。在代碼中使用JT400的IFS文件副本

public void CopyFile(AS400 system, String source, String destination){ 
    File destFile = new File(destination); 
    IFSFile sourceFile = new IFSFile(system, source); 
    if (!destFile.exists()){ 
     try { 
      destFile.createNewFile(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
      IFSFileInputStream in = null; 
    OutputStream out = null; 
    try { 
     in = new IFSFileInputStream(sourceFile); 
     out = new FileOutputStream(destFile); 

      // Transfer bytes from in to out 
      byte[] buf = new byte[1024]; 
      int len; 
      while ((len = in.read(buf)) > 0) { 
       out.write(buf, 0, len); 
      } 
     } catch (AS400SecurityException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if(in != null) { 
        in.close(); 
       } 
       if(out != null) { 
        out.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } // end try catch finally 

} // end method 

  • 源=全IFS路徑+文件名和
  • 目的地=完整本地路徑+文件名

我想問一下關於以下一些事情:

  • a。性能考慮因素

    1. 這會對主機AS400系統的CPU使用率產生很大影響嗎?
    2. 這會對使用的JVM產生很大的影響(根據內存使用情況)
    3. 會將這種情況包含到Web應用程序中,從而影響應用程序服務器性能(這是否是一項沉重的任務)?
    4. 將使用此複製多個文件(冗餘運行)是所有涉及的資源的一大負擔?
  • b。代碼質量

    1. 我的IFSFileInputStream實現是否足夠了,還是一個簡單的FileInputStream對象能很好地完成這項工作?

據我所知,我只是需要AS400對象,以確保所引用的源文件是從IFS文件。

我是AS400和IFS的noob,想問一個有經驗的人的誠實意見。

回答

1

總而言之,它看起來很好(沒有嘗試)。它不應該有明顯的影響。

  • in.read()可能會返回0.測試-1代替。
  • 而不是手動緩衝,只需用它們各自的BufferedInputStream/BufferedOutputstream進行換行並讀取一個字符並測試它爲-1。
  • try-catch很難變得漂亮。這會做,但你以後會獲得更多的經驗,並學習如何做得更好。
  • 不是吞下異常並打印它們。調用你的代碼將不知道它是否順利。
  • 完成AS400對象時,請使用as400.disconnectAllServices()。
+0

謝謝你這麼多的快速回答托爾比約恩。我也想到了as400.disconnectAllServices()部分,這將是我的類的清理方法(銷燬)的一部分。是的,我希望我很快能找到一種更好地處理try-catch塊的方法。我還計劃使用log4j而不是使用printStackTrace來記錄異常。但是,如何處理異常並很好地通知調用代碼?再次感謝。 – aEtherv0id