迟醉_ 2022-09-07 14:59 采纳率: 86.8%
浏览 26
已结题

Java复制图片到其他路径


@Test
    public void input() {
        String filePath1 = "e:\\tupian.jpg";
        String filePath2 = "d:\\tupian.jpg";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        int readLen = 0;
        byte[] b = new byte[10240];
        try {
            fileInputStream = new FileInputStream(filePath1);
            fileOutputStream = new FileOutputStream(filePath2);
           while ((readLen = fileInputStream.read(b)) != -1) {
                fileOutputStream.write(new String(b,0, readLen).getBytes(), 0, readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
@Test
    public void input() {
        String filePath1 = "e:\\tupian.jpg";
        String filePath2 = "d:\\tupian.jpg";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        int readLen = 0;
        byte[] b = new byte[10240];
        try {
            fileInputStream = new FileInputStream(filePath1);
            fileOutputStream = new FileOutputStream(filePath2);
           while ((readLen = fileInputStream.read(b)) != -1) {
                fileOutputStream.write(new String(b,0, readLen).getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

@Test
    public void input() {
        String filePath1 = "e:\\tupian.jpg";
        String filePath2 = "d:\\tupian.jpg";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        int readLen = 0;
        byte[] b = new byte[10240];
        try {
            fileInputStream = new FileInputStream(filePath1);
            fileOutputStream = new FileOutputStream(filePath2);
           while ((readLen = fileInputStream.read(b)) != -1) {
                fileOutputStream.write(b, 0, readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

只有第三个可以完成复制,不知道前两个错误的原因

  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-09-07 15:26
    关注

    String可能内部把二进制数据当字符串,进行编码转换了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月28日
  • 已采纳回答 9月20日
  • 创建了问题 9月7日

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程