jdk1.7新特性
主要是
1.switch可以接受string类型而不像以前仅仅是int;
2.异常catch可以一次处理完而不像以前一层层的surround;
3.泛型类实例化也不用繁琐的将泛型声明再写一遍;
4.文件读写 会自动关闭流而不像以前那样需要在finally中显式close;
5.数值可以使用下划线分隔;
6.文件读写功能增强,有更简单的api调用;
7.文件改变的事件通知功能;
8.多核 并行计算的支持加强 fork join 框架;
9.还有一些动态特性的加入。
1 很简单就不举例子了
2
public void newMultiCatch() {
try {
methodThatThrowsThreeExceptions();
} catch (ExceptionOne | ExceptionTwo | ExceptionThree e) {
// log and deal with all Exceptions
}
}
3 Map<String, List<Trade>> trades = new TreeMap <> ();
4 public void newTry() {
try (FileOutputStream fos = new FileOutputStream("movies.txt");
DataOutputStream dos = new DataOutputStream(fos)) {
dos.writeUTF("Java 7 Block Buster");
} catch (IOException e) {
// log the exception
}
}
5 int million = 1_000_000
6
public void pathInfo() { Path path = Paths.get("c:\\Temp\\temp"); System.out.println("Number of Nodes:" + path.getNameCount()); System.out.println("File Name:" + path.getFileName()); System.out.println("File Root:" + path.getRoot()); System.out.println("File Parent:" + path.getParent()); //这样写不会抛异常 Files.deleteIfExists(path); }
7
package com.he; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.util.List; public class Main { public static void main(String[] args) { //define a folder root Path myDir = Paths.get("H:/Temp"); while(true){ try { WatchService watcher = myDir.getFileSystem().newWatchService(); myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); WatchKey watckKey = watcher.take(); List<WatchEvent<?>> events = watckKey.pollEvents(); for (WatchEvent event : events) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { System.out.println("Created: " + event.context().toString()); } if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) { System.out.println("Delete: " + event.context().toString()); } if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { System.out.println("Modify: " + event.context().toString()); } } } catch (Exception e) { System.out.println("Error: " + e.toString()); } } } }