mm无捷之路 2020-09-11 10:13 采纳率: 33.3%
浏览 99
已采纳

请问java如何复制gif图片

以下代码经测试,可以支持中文处理,并且可以复制多种类型,比如txt,xml,jpg,doc等多种格式,那么gif图片复制后会损坏,请问怎么操作

public void copyFile(String src,String dest) throws IOException{
         FileInputStream in=new FileInputStream(src);
         File file=new File(dest);
         if(!file.exists())
             file.createNewFile();
         FileOutputStream out=new FileOutputStream(file);
         int c;
         byte buffer[]=new byte[1024];
         while((c=in.read(buffer))!=-1){
             for(int i=0;i<c;i++)
                 out.write(buffer[i]);        
         }
         in.close();
         out.close();
     }
  • 写回答

2条回答 默认 最新

  • thomas.he 2020-09-11 11:04
    关注

    这个是我们项目上用过的

    public static void copyFile(String newPath,String oldPath) throws Exception{
            InputStream inStream = null;
            FileOutputStream fs = null;
            try{
                int bytesum = 0;
                int byteread = 0;
                File oldfile = new File(oldPath); 
                if (oldfile.exists()) { //文件存在时 
                    inStream = new FileInputStream(oldPath); //读入原文件 
                    fs = new FileOutputStream(newPath); 
                    byte[] buffer = new byte[1444]; 
                    while ( (byteread = inStream.read(buffer)) != -1) { 
                        bytesum += byteread; //字节数 文件大小
                        System.out.println(bytesum);
                        fs.write(buffer, 0, byteread);
                    }
                }
            }catch (Exception e) {
                System.out.println("复制单个文件操作出错");
                e.printStackTrace();
                throw e;
            }finally {
                if(inStream != null){
                    inStream.close();
                }
                if(fs != null){
                    fs.close();
                }
            }
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办