2014-05-13 162 views
-1

我有一個圖片流(目前是一個PNG,但它可能是別的東西),我想將它轉換爲一個文件。 問題:流不entierely閱讀: 從中流來的初始畫面約20KB,而壓軸的文件是16架B 這裏是我的代碼:Java輸入流沒有完全讀取

... 
    fileImageOutput = new FileImageOutputStream(_templateFile);   

    int read = 0; 
    byte[] bytes = new byte[1024]; 

    while ((read = imageStream.read(bytes)) != -1) { 
     fileImageOutput.write(bytes, 0, read); 
    } 
} 
catch(IOException ioe) 
{ 
    throw ioe; 
} 
finally 
{ 
    if(fileImageOutput!=null) 
     fileImageOutput.close(); 
    if(imageStream!=null) 
     imageStream.close(); 
} 

非常感謝你的幫助。

薩科

+0

在其中創建您的輸入流可以插入代碼?還有你做任何檢查,看看這16個字節是什麼,看看他們是否與圖像文件的前16個字節? –

+0

首先讀取文件的內容並在屏幕上打印並打開使用的文件檢查內容的差異 –

+1

在關閉文件之前對流進行調用沖洗 – Stefan

回答

0

當你寫一個文件,你應該使用FileOutputStream中,而不是FileImageOutputStream ..作家寫字節,不管他們是。如果我們在try/catch塊中看到完整的代碼,這將更加清楚。

... 
BufferedWriter writer = new BufferedWriter(new FileOutputStream(_templateFile));  
int read; 
while ((read = imageStream.read()) != -1) { 
    writer.write(read); 
} 
... 
0

感謝您的回答。 其實,我發現了這個問題:我混合兩種方式來加載圖片,並且都使用了輸入流。自從我從第一個開始,我認爲這改變了它的索引,並且在最後開始了對該流的第二次讀取。無論如何,我刪除了第二次閱讀,現在它可以工作!

但是感謝

尼科