A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 淡淡柠檬茶 高级黑马   /  2014-6-6 12:49  /  1108 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//复制一个图片
import java.io.*;
class  CopyPic
{
        public static void main(String[] args) throws IOException
        {
                /*方法1
                FileOutputStream ou = new FileOutputStream("C:\\.2.jpg");
                FileInputStream in = new FileInputStream("C:\\1.jpg");
                int num = 0;
                while ((num = in.read())!=-1)
                {
                        ou.write(num);
                }
                ou.close();
                in.close();
                */


                //方法2
                FileOutputStream ou = new FileOutputStream("C:\\.2.jpg");
                FileInputStream in = new FileInputStream("C:\\1.jpg");
                byte[] bvf = new byte[in.available()];
                in.read(bvf);
                ou.write(bvf);
               
                ou.close();
                in.close();


                /*方法3
                FileOutputStream ou = null;
                FileInputStream in = null;
                try
                {
                        ou = new FileOutputStream("c:\\.2.jpg");
                        in = new FileInputStream("c:\\1.jpg");
                        
                        //byte[] bvf = new byte[1024];
                        //int len = 0;
                        //while((len = in.read(bvf))!=-1)
                        //{
                        //        ou.write(bvf,0,len);
                        //}
                        
                        byte[] bvf = new byte[in.available()];
                        in.read(bvf);
                        ou.write(bvf);
               
                }
                catch (Exception e)
                {
                        throw new RuntimeException("复制失败");
                }
                finally
                {
                        if(in!=null)
                        try
                        {
                                in.close();
                        }
                        catch (Exception e)
                        {
                                throw new RuntimeException("读取失败");
                        }
                        if(ou!=null)
                        try
                        {
                                ou.close();
                        }
                        catch (Exception e)
                        {
                                throw new RuntimeException("写入失败");
                        }
                }
                */
        }
}

2 个回复

倒序浏览
对了,方法2中可以不采用available()方法,这样的话就有4种了
回复 使用道具 举报
好的原来可以这样!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马