NIO - java new io2 since jdk7

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);
    }
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值