黑马程序员技术交流社区
标题:
io流遇到的问题,想的头疼
[打印本页]
作者:
魏亮
时间:
2012-10-12 16:39
标题:
io流遇到的问题,想的头疼
本帖最后由 魏亮 于 2012-10-12 16:42 编辑
import java.io.*;
class CopyPic
{
public static void main(String[] args)
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream("pic.bmp");
fos = new FileOutputStream("pic_2.bmp");
<FONT color=yellowgreen> int ch = 0;
while ((ch=fis.read())!=-1)
{
fos.write(ch);
}
</FONT> <FONT color=red>//上边的的方法程序停止,光标闪烁,复制的图片文件无法读取,为什么? 我检查了很多遍,实在想不出??
/*下边的方法可以复制成功
</FONT> <FONT color=yellowgreen> byte[] buf = new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
</FONT> <FONT color=red> * /
</FONT> catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
if(fis!=null)
fis.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
try
{
if(fos!=null)
fis.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
复制代码
作者:
张 涛
时间:
2012-10-12 16:47
write方法后面要及时flush,才能写到文件中。
作者:
徐梦侠
时间:
2012-10-12 18:01
代码没有问题吧,你的代码除了第一个try没有结束的}外,代码都能够正常运行,复制的图片没有问题,你再重新复制出来试一下呢。
作者:
杨志男
时间:
2012-10-12 20:18
package com.ithima.practice;
import java.io.*;
class CopyPic {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("pic.bmp");
fos = new FileOutputStream("pic_2.bmp");
int ch = 0;
while ((ch = fis.read()) != -1) {
fos.write(ch);
}
}
/*
* byte[] buf = new byte[1024]; int len=0;
* while((len=fis.read(buf))!=-1) {fos.write(buf,0,len); }
*/
catch (IOException e) {
System.out.println(e.toString());
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
System.out.println(e.toString());
}
try {
if (fos != null)
fis.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
复制代码
完全可以啊,亲,亲测的
作者:
魏亮
时间:
2012-10-15 09:42
可以????我再试试
作者:
魏亮
时间:
2012-10-15 10:39
这个真的没有问题,我等了一段时间就复制成功了,肯能是我电脑太慢了,劳驾上面各位
作者:
陈军
时间:
2012-10-15 11:36
上面没有用flush方法刷新。。下面有close(),这个方法关闭前会有刷新动作 {:soso_e100:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2