K20230110 - JAVA -字符流拷贝图片/网络图片+集合遍历

问题1

public class q1 {
    /*
    1.拷贝一张图片,到另外一个目录下
     */
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("E:\\TEXT\\bee.png");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream("E:\\TEXT1\\bee_copy.png");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        byte[] bytes = new byte[8096];
        while (bis.read(bytes) != -1) bos.write(bytes);

        bis.close();
        bos.close();
        fis.close();
        fos.close();
    }
}

问题2

public class q2 {
    /*
       2.在网上找一张图片,复制图片的网络地址,然后写一个Java程序下载该图片到本地。
       提示:
     	   // 获取网页图片
       URL url1 = new URL("https://img-blog.csdnimg.cn/41f84da3459a42f49b574f6732f69e08.png
       ?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,
       text_Q1NETiBA5o6S6aqo546J57Gz5rGk,size_20,color_FFFFFF,t_70,g_se,x_16");

       URLConnection uc = url1.openConnection();
       InputStream inputStream = uc.getInputStream();
    */
    public static void main(String[] args) throws Exception {
        URL url1 = new URL("https://img-blog.csdnimg.cn/41f84da3459a42f49b574f6732f69e08.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5o6S6aqo546J57Gz5rGk,size_20,color_FFFFFF,t_70,g_se,x_16");
        URLConnection uc = url1.openConnection();
        InputStream inputStream = uc.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        FileOutputStream fos = new FileOutputStream("E:\\TEXT\\download.png");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] bytes = new byte[8096];
        int read = -1;
        while ((read = bis.read(bytes)) != -1) bos.write(bytes, 0,read );
        bis.close();
        bos.close();
        inputStream.close();
        fos.close();
    }
}

问题3

public class q3 {
    /*
     * 3. 有2个数组,第一个数组内容为:[黑龙江省,浙江省,江西省,广东省,福建省],
     * 第二个数组为:[哈尔滨,杭州,南昌,广州,福州],将第一个数组元素作为key,
     * 第二个数组元素作为value存储到Map集合中。如{黑龙江省=哈尔滨, 浙江省=杭州, …}。

     * */
    public static void main(String[] args) {
        String[] keys = {"黑龙江省", "浙江省", "江西省", "广东省", "福建省"};
        String[] value = {"哈尔滨", "杭州", "南昌", "广州", "福州"};
        Map<String,String> map = new HashMap<>();
        for (int i = 0; i < keys.length; i++) {
            map.put(keys[i],value[i]);
        }
    }
}

问题4

public class q4 {
    /*
     * 4. 创建一个List集合的对象,添加几条数据,将1号位和2号位交换;获得最大值,最小值打印出来,最后再遍历该集合并把元素打印出来
     * */
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++)
            list.add((int) (Math.random() * 50));
        // 1号位和2号位交换
        list.set(1, list.get(2));
        list.set(2, list.get(1));
        // 最大值,最小值
        int max = list.get(0);
        int min = list.get(0);
        for (int i : list) {
            max = Math.max(max,i);
            min = Math.min(min,i);
        }
        System.out.println("max = " +max);
        System.out.println("min = " +min);
        // 遍历
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext())
            System.out.print(iterator.next()+" ");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值