黑马程序员技术交流社区
标题:
字节流问题,请大虾帮忙
[打印本页]
作者:
任我行
时间:
2014-12-16 17:23
标题:
字节流问题,请大虾帮忙
代码是用于复制一张图片,运行成功,但复制出来的图片字节为0,不能打开。。
public class CopyPic
{
public static void main(String[] args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fos = new FileOutputStream("C:\\2.jpg");
fis = new FileInputStream("C:\\1.jpg");
byte[] buf = new byte[1024];
int lenght = 0;
while (-1 !=(fis.read(buf)))
{
fos.write(buf,0,lenght);
}
} catch (IOException e)
{
throw new RuntimeException("复制文件失败");
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
} catch (IOException e2)
{
throw new RuntimeException("读取文件失败");
}
try
{
if (fos != null)
{
fos.close();
}
} catch (IOException e2)
{
throw new RuntimeException("写入文件失败");
}
}
}
}
复制代码
作者:
huoxy
时间:
2014-12-16 17:28
lenght一直是0,所以复制失败!应该是while(-1!=(lenght = fis.read(buf)))! 另:长度应该是length单词(强迫症,伤不起)。
作者:
王世虎
时间:
2014-12-16 18:40
楼上哥们正解,我是这个length怎么看着难受 哈哈
作者:
任我行
时间:
2014-12-17 09:33
哈哈哈 多谢指点:handshake
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2