2013-03-23 37 views
0
 int count = 0; 
     byte[] buffer = new byte[8192]; // more if you like but no need for 
     int j = length_img % 8192; 
     int value = length_img - j; 
     int incre = 0;// it to be the entire file size 

     for (int i = 0; i < (value/8192) + 1; i++) { 

      if (length_img == incre + j) { 
       buffer = new byte[j]; 
       int pair = dataInputStream.read(buffer); 
       System.out.print("i am here for last chunk" + buffer.length 
         + "value of j is" + j + "incre value is" + incre 
         + "the value of i is" + i 
         + "and the value of count is" + pair); 

       image0OutFile.write(buffer, 0, pair); 
       break; 
      } 

      else { 
       count = dataInputStream.read(buffer); 
       image0OutFile.write(buffer, 0, count); 
       incre += 8192; 
      } 
     } 

這正是一些問題lies.i正在此輸出length_img部分:3147850 [B @ 72d86c58 我在這裏對於j is2122incre價值的最後chunk2122value我的is3145728the值is384and count的值是496bublo ...如果你注意到pair的值應該是2122,而是它的496,這就是文件爲什麼會破壞的原因。寫完整的內容到圖像文件

回答

2

你應該關閉FileOutputStream

image0OutFile.close

這將刷新最後部分的文件。或者你甚至可以撥打:

image0OutFile.flush()

但前一個建議。你應該總是關閉你打開的流。

+0

我正在關閉流沒有包括在代碼中,但我這樣做。問題是要更精確的值對image0OutFile.write(緩衝區,0,一對)的對變化;這裏對的值與buffer.length.i相比有所改變,只是在打印後檢查它 – 2013-03-23 10:04:50

0

始終關閉流

把你的代碼中Try catchFinally塊first.Close流。

finally{ 
     image0OutFile.close(); 
     } 

而不要去flush.When關閉時調用自動刷新發生。

相關問題