代码是用于复制一张图片,运行成功,但复制出来的图片字节为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("写入文件失败");
- }
-
- }
- }
- }
复制代码 |
|