jdk7和8的一些新特性介绍 (二)

2014-11-24 10:21:36 · 作者: · 浏览: 1
; // OK (decimal literal) //int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix //int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number int x7 = 0x5_2; // OK (hexadecimal literal) //int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number int x9 = 0_52; // OK (octal literal) int x10 = 05_2; // OK (octal literal) //int x11 = 052_; // Invalid; cannot put underscores at the end of a number 1.6 泛型实例的创建可以通过类型推断来简化 可以去掉后面new部分的泛型类型,只用<>就可以了。 //使用泛型前 List strList = new ArrayList(); List strList4 = new ArrayList(); List>> strList5 = new ArrayList>>(); //编译器使用尖括号 (<>) 推断类型 List strList0 = new ArrayList(); List>> strList1 = new ArrayList>>(); List strList2 = new ArrayList<>(); List>> strList3 = new ArrayList<>(); List list = new ArrayList<>(); list.add("A"); // The following statement should fail since addAll expects // Collection< extends String> //list.addAll(new ArrayList<>()); 1.7在可变参数方法中传递非具体化参数,改进编译警告和错误 Heap pollution 指一个变量被指向另外一个不是相同类型的变量。例如 List l = new ArrayList(); List ls = l; // unchecked warning l.add(0, new Integer(42)); // another unchecked warning String s = ls.get(0); // ClassCastException is thrown Jdk7: public static void addToList (List listArg, T... elements) { for (T x : elements) { listArg.add(x); } } 你会得到一个warning warning: [varargs] Possible heap pollution from parameterized vararg type 要消除警告,可以有三种方式 1.加 annotation @SafeVarargs 2.加 annotation @SuppressWarnings({"unchecked", "varargs"}) 3.使用编译器参数 –Xlint:varargs; 1.8 信息更丰富的回溯追踪 就是上面try中try语句和里面的语句同时抛出异常时,异常栈的信息 java.io.IOException § at Suppress.write(Suppress.java:19) § at Suppress.main(Suppress.java:8) § Suppressed: java.io.IOException § at Suppress.close(Suppress.java:24) § at Suppress.main(Suppress.java:9) § Suppressed: java.io.IOException § at Suppress.close(Suppress.java:24) § at Suppress.main(Suppress.java:9) 2. NIO2的一些新特性 1.java.nio.file 和java.nio.file.attribute包 支持更详细属性,比如权限,所有者 2. symbolic and hard links支持 3. Path访问文件系统,Files支持各种文件操作 4.高效的访问metadata信息 5.递归查找文件树,文件扩展搜索 6.文件系统修改通知机制 7.File类操作API兼容 8.文件随机访问增强 mapping a region,locl a region,绝对位置读取 9. AIO Reactor(基于事件)和Proactor 下面列一些示例: 2.1IO and New IO 监听文件系统变化通知 通过FileSystems.getDefault().newWatchService()获取watchService,然后将需要监听的path目录注册到这个watchservice中,对于这个目录的文件修改,新增,删除等实践可以配置,然后就自动能监听到响应的事件。 private WatchService watcher; public TestWatcherService(Path path) throws IOException { watcher = FileSystems.getDefault().newWatchService(); path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); } public void handleEvents() throws InterruptedException { while (true) { WatchKey key = watcher.take(); for (WatchEvent< >
event : key.pollEvents()) { WatchEvent.Kind kind = event.kind(); if (kind == OVERFLOW) {// 事件可能lost or discarded continue; } WatchEvent e = (WatchEvent) event; Path fileName = e.context(); System.out.printf("Event %s has happened,which fileName is %s%n",kind.name(), fileName); } if (!key.reset()) { break; } 2.2 IO and New IO遍历文件树 ,通过继承SimpleFileVisitor类,实现事件遍历目录树的操作,然后通过Files.walkFileTree(listDir, opts, Integer.MAX_VALUE, walk);这个API来遍历目录树 private void wo