迟醉_ 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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题