new feature compared with old java.io package
* real support file operation across platforms
* real support for symbolic links
* more support on file metadata , such as permission, owner and other security options
go to below to see some code example How to Explore File Attributes Using 'Files' class:
http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html
Snapshot:
package com.pc.tmp;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileOwnerInJdk7 {
/**
* @param args
*/
public static void main(String[] args) {
String str = "F:/workspace/ws/tmp/pom.xml";
Path path = Paths.get(str);
try {
System.out.println("The owner of file " + str + " is " + Files.getOwner(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output: The owner of file F:/workspace/ws/tmp/pom.xml is \Everyone (Well-known group)
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());
System.out.println("isDirectory: " + attr.isDirectory());System.out.println("isOther: " + attr.isOther());
System.out.println("isRegularFile: " + attr.isRegularFile());
System.out.println("isSymbolicLink: " + attr.isSymbolicLink());
System.out.println("size: " + attr.size());
* can help you recursive a file tree and respond appropriately if there are circular symbolic links
Introduction by Sun in 2008 JavaOne:
http://www.youtube.com/watch?v=yNRS1ssLPdQ
More ref links about non-blocking:
http://stackoverflow.com/questions/8777743/making-sense-of-javas-non-blocking-i-o
http://rox-xmlrpc.sourceforge.net/niotut/
http://tutorials.jenkov.com/java-nio/nio-vs-io.html
Demo with diagram : http://kylinsoong.iteye.com/blog/1294482 (but the example has buffer exceed problem... ByteBuffer problem.)
http://docs.oracle.com/javase/1.4.2/docs/guide/nio/example/
引用这里的oracle 官方例子,Ping.java, 测试以下服务器(80 baidu.com google.com 212.111.12.0 www.kuanzone.com sina.com 163.com cntv.com hsbc.com icbc.com abc.com huawei.com narutom.com)
www.kuanzone.com/59.42.214.242:80 : 36ms
google.com/74.125.128.100:80 : 47ms
baidu.com/220.181.111.85:80 : 70ms
第一个是局域网内的网址。测试好几次,google挺强大,网速都快过baidu.....?! +_+
Findings:
boolean connected = sc.connect(t.address);
PLEASE NOTE before "connect()", if "t.address" is a string (hostname not IP) , it will doDNS querywhich is inblocking mode and when the host (e.g. com.ab) can't be resolved to IP address then cause unhandled exception in that app.
About ByteBuffer's method (can refer to http://tutorials.jenkov.com/java-nio/buffers.html) capacity, limit , position
flip() - reverse the buffer before read
compact() - Put the unread elements to the beginning clear() - clear all elements, you may lose the unread data
rewind() - for re-read purpose
mark() - set a marker in the buffer reset() - go back to the marker
An example codes:
public static final void nioCopy(ReadableByteChannel input, WritableByteChannel output) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
while (input.read(buffer) != -1) {
//Flip buffer
buffer.flip();
//Write to destination
output.write(buffer);
//Compact
buffer.compact();
}
//In case we have remainder
buffer.flip();
while (buffer.hasRemaining()) {
//Write to output
output.write(buffer);
}
}