2014-05-13 74 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

感谢您的回答。 其实,我发现了这个问题:我混合两种方式来加载图片,并且都使用了输入流。自从我从第一个开始,我认为这改变了它的索引,并且在最后开始了对该流的第二次读取。无论如何,我删除了第二次阅读,现在它可以工作!

但是感谢

尼科