2015-11-27 60 views
0

我想從數據庫中使用Java JDBC進行查詢,並將數據壓縮到一個列中的特定目錄中的gzip文件。我已經測試了我的JDBC查詢並且工作正常,但Gzip代碼沒有和while循環一起運行,它與循環firt行一起運行並停留在那裏。爲什麼卡住了?請幫幫我! 這些文件夾已經存在:d:\目錄\我\一年\ ID1 \ ID2gzip壓縮器不會與while循環一起

//Some query JDBC code here, it's work well. I query all rows Data, year, id1,id2,id3 
while (myRs1.next()){ 

     String str = Data; 
    File myGzipFile = new File("D:\\Directory\\My\\"+year+"\\"+id1+"\\"+id2+"\\"+id3+".gzip"); 

    GZIPOutputStream gos = null; 
    InputStream is = new ByteArrayInputStream(str.getBytes()); 
    gos = new GZIPOutputStream(new FileOutputStream(myGzipFile)); 

    byte[] buffer = new byte[1024]; 
    int len; 
    while ((len = is.read(buffer)) != -1) { 
     gos.write(buffer, 0, len); 
     System.out.print("done for:"+id3); 
    } 


    try { gos.close(); } catch (IOException e) { } 
} 
+0

它如何卡住?什麼是錯誤。爲什麼不嘗試打印IOException – Han

+0

不好意思,我的意思是Gzip代碼讓循環在運行第一行時停止。結果是1個文件Gzip被創建並且:「構建成功(總時間:0秒)」 –

+0

我認爲它卡在這裏:因爲數據是空的。但如何讓它超過空數據? while((len = is.read(buffer))!= -1)gos.write(buffer,0,len); System.out.print(「done for:」+ id3); } –

回答

0

嘗試格式化這樣的源捕獲異常。

public class InputStreamDemo { 
    public static void main(String[] args) throws Exception { 

     InputStream is = null; 
     int i; 
     char c; 

     try{ 
     is = new FileInputStream("C://test.txt"); 

     System.out.println("Characters printed:"); 

     // reads till the end of the stream 
     while((i=is.read())!=-1) 
     { 
      // converts integer to character 
      c=(char)i; 

      // prints character 
      System.out.print(c); 
     } 
     }catch(Exception e){ 

     // if any I/O error occurs 
     e.printStackTrace(); 
     }finally{ 

     // releases system resources associated with this stream 
     if(is!=null) 
      is.close(); 
     } 
    } 
}