JDK9-13新特性展示(一)— JDK9

       所有的说明和代码都在一起,直接放代码,大家就可以很清楚的看到了。没有列出所有的新特性,只是列出了基本开发中可能会用到的新特性

     

public class NineNewTest {

    /**不可变集合工厂方法
     * Java 9增加了List.of()、Set.of()、Map.of()和Map.ofEntries()等工厂方法来创建不可变集合。
     * List strs = List.of("Hello", "World");
     * List strs List.of(1, 2, 3);
     * Set strs = Set.of("Hello", "World");
     * Set ints = Set.of(1, 2, 3);
     * Map maps = Map.of("Hello", 1, "World", 2);
    **/
    public static void testFinalCollections(){
        List<String> aaa = List.of("aaa", "bbb");
        Map<String, Integer> hello = Map.of("Hello", 1, "World", 2);
        System.out.println(aaa.toString());
        System.out.println(hello);
    }

    /**
     * I/O 流新特性
     * java.io.InputStream 中增加了新的方法来读取和复制 InputStream 中包含的数据。
     *    readAllBytes:读取 InputStream 中的所有剩余字节。
     *    readNBytes: 从 InputStream 中读取指定数量的字节到数组中。
     *    transferTo:读取 InputStream 中的全部字节并写入到指定的 OutputStream 中
    **/
    public void testIO(){
        try(InputStream resourceAsStream = this.getClass().getResourceAsStream("1.txt");
        OutputStream outputStream = new FileOutputStream(new File("d:\\2.txt"))) {
            // InputStream stream = new BufferedInputStream(resourceAsStream);
            byte[] bytes;
            // bytes  = resourceAsStream.readAllBytes();
            // bytes = resourceAsStream.readNBytes(10);
            resourceAsStream.transferTo(outputStream);
            // System.out.println(new String(bytes));
            outputStream.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }



    public static void main(String[] args) throws IOException {
        // NineNewTest.testFinalCollections();
        NineNewTest test = new NineNewTest();
        test.testIO();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值