前言
最近在ITeye上看见一些朋友正在激烈讨论关于Java7.x的一些语法结构,所以笔者有些手痒,特此探寻了7.x(此篇博文笔者使用的是目前最新版本的JDK-7u15)的一些新特性分享给大家。虽然目前很多开发人员至今还在沿用Java4.x(笔者项目至今沿用4.x),但这并不是成为不前进的借口。想了解Java的发展,想探寻Java的未来,那么你务必需要时刻保持一颗永不落后的心。
当然笔者此篇博文并不代表官方观点,如果有朋友觉得笔者的话语是妙论,希望指正提出,笔者会在第一时间纠正博文内容。在此笔者先谢过各位利用宝贵的时间阅读此篇博文,最后笔者祝愿各位新年大吉,工作顺利。再次啰嗦一点,SSJ的系列博文,笔者将会在本周更新,希望大家体谅。
目录
一、自动资源管理;
二、“<>”类型推断运算符;
三、字面值下划线支持;
四、switch字面值支持;
五、声明二进制字面值;
六、catch表达式调整;
七、文件系统改变;
八、探讨Java I/O模型;
九、Swing Framework(JSR 296规范)支持;
十、JVM内核升级之Class Loader架构调整;
十一、JVM内核升级之Garbage Collector调整(时间仓促,后期讲解);
package com.test;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class NIO2WatchService {
//WatchService 是线程安全的,跟踪文件事件的服务,一般是用独立线程启动跟踪
public void watchRNDir(Path path) throws IOException, InterruptedException {
try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
//给path路径加上文件观察服务
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE);
// start an infinite loop
while (true) {
// retrieve and remove the next watch key
final WatchKey key = watchService.take();
// get list of pending events for the watch key
for (WatchEvent<?> watchEvent : key.pollEvents()) {
// get the kind of event (create, modify, delete)
final Kind<?> kind = watchEvent.kind();
// handle OVERFLOW event
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
//创建事件
if(kind == StandardWatchEventKinds.ENTRY_CREATE){
}
//修改事件
if(kind == StandardWatchEventKinds.ENTRY_MODIFY){
}
//删除事件
if(kind == StandardWatchEventKinds.ENTRY_DELETE){
}
// get the filename for the event
final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
final Path filename = watchEventPath.context();
// print it out
System.out.println(kind + " -> " + filename);
}
// reset the keyf
boolean valid = key.reset();
// exit loop if the key is not valid (if the directory was
// deleted, for
if (!valid) {
break;
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
final Path path = Paths.get("C://logs");
NIO2WatchService watch = new NIO2WatchService();
try {
watch.watchRNDir(path);
} catch (IOException | InterruptedException ex) {
System.err.println(ex);
}
}
}
http://gao-xianglong.iteye.com/blog/1819748
http://blog.youkuaiyun.com/zhongweijian/article/details/9258997
https://github.com/zhwj184/jdk7-8demo
本文探讨了Java 7的一些关键新特性,包括自动资源管理、类型推断、字面值支持等,并深入介绍了NIO 2中WatchService的使用方法。

被折叠的 条评论
为什么被折叠?



