[java]nio.2 施用续

[java]nio.2 使用续

第一篇在这里:http://fair-jm.iteye.com/blog/1912942

这里再记录一些

 

SymbolicLink无法作用在win的快捷方式

符号引用简单点说就像个快捷方式(shortcut) 但是在使用中发现这对win下的快捷方式无效:

代码如下(混杂了其他东西):

	Path path=Paths.get("../BF.png.lnk");	
	path=path.toRealPath();
	System.out.println(path);
	System.out.println(Files.isSymbolicLink(path));
	//符号链接在win下不能用
    BasicFileAttributeView bav=Files.getFileAttributeView(path, BasicFileAttributeView.class);
    
    System.out.println("startWith?:"+path.startsWith(Paths.get("G:/nio2")));
    System.out.println("endsWith?:"+path.endsWith(Paths.get("BF.png.lnk")));
    
    BasicFileAttributes ba=bav.readAttributes();
    //或者:
    //BasicFileAttributes ba=Files.readAttributes(file, BasicFileAttributes.class);
    System.out.format("File size: %s \n" , ba.size());  
    System.out.format("File creation time: %s \n" , ba.creationTime());  
    System.out.format("File was last accessed at: %s \n" , ba.lastAccessTime());  
    System.out.format("File was last modified at: %s \n" , ba.lastModifiedTime());  
    System.out.format("Is directory? %s \n" , ba.isDirectory());  
    System.out.format("Is regular file? %s \n" , ba.isRegularFile());  
    System.out.format("Is symbolic link? %s \n" , ba.isSymbolicLink());  
    System.out.format("Is other? %s \n" , ba.isOther());  

 输出:

G:\nio2\BF.png.lnk
false
startWith?:true
endsWith?:true
File size: 935 
File creation time: 2013-08-13T15:00:13.162298Z 
File was last accessed at: 2013-08-13T15:00:13.2023Z 
File was last modified at: 2013-08-13T15:00:21.9528Z 
Is directory? false 
Is regular file? true 
Is symbolic link? false 
Is other? false 

 可见shortcut不算符号引用(文件的真实路径显示的还是G:\nio2\BF.png.lnk) 也无法得到链接的文件(这边输出的文件信息就是那个快捷方式的)

 

当然win也不是那么鸡肋的 win7下提供了mklink的工具 可以创造符号链接 

命令如下(win7请以管理员模式运行cmd):

G:\nio2>mklink BFSym.png C:\Users\dell\Desktop\BF.png

 当然还有额外的参数可以用 具体可以参考:

http://www.cnblogs.com/asion/archive/2011/03/10/1979282.html

然后我把以上代码里的BF.png.lnk换成BGSym.png 再看输出:

C:\Users\dell\Desktop\BF.png
false
startWith?:false
endsWith?:false
File size: 158701 
File creation time: 2012-08-27T08:08:04.561389Z 
File was last accessed at: 2012-10-11T11:17:25.613585Z 
File was last modified at: 2012-08-27T08:08:04.635393Z 
Is directory? false 
Is regular file? true 
Is symbolic link? false 
Is other? false 

 可以看到已经是连接的文件信息了

 

关于使用的toRealPath()方法:

java程序员修炼之道 写道
此外,toRealPath()方法也很有效,它融合了toAbsolutePath()和normalize()两个方法的功能,还能检测并跟随符号连接。

 

此外还有一点要注意

 BasicFileAttributeView bav=Files.getFileAttributeView(path, BasicFileAttributeView.class);

默认会跟随符号连接的 代码我不再演示 如果只是要读符号连接本身(而不是连接的文件)的属性

记得加个参数:

 BasicFileAttributeView bav=Files.getFileAttributeView(path, BasicFileAttributeView.class,LinkOption.NOFOLLOW_LINKS);

 

 

 

 

WatchService使用:

在原来没有写 这边写个简单的例子:

	public static void main(String[] args) throws IOException, InterruptedException {
		Path file=Paths.get(".").toRealPath(); //G:\nio2\path_test
		Path watchDir=file.resolveSibling("watchDir"); //G:\nio2\path\watchDir
		System.out.println(watchDir.toRealPath());
		
		WatchService ws=FileSystems.getDefault().newWatchService();
		
		WatchKey wk=watchDir.register(ws, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY);
		System.out.println("watching......");
		while(true){
			wk=ws.take();
			for(WatchEvent event:wk.pollEvents()){
				System.out.println((new Date())+":"+((Path)event.context()).getFileName().toString()+":"+event.kind());
			}
			wk.reset();
		}
	}

 这个只能检测一个目录 不检测子目录的变化 如果子目录下产生文件了 他只会简单返回一个子目录被修改的事件 这边对文件的重命名也比较有意思 返回的事件是原名的文件被删除和新名的文件被创建:

G:\nio2\watchDir
watching......
Wed Aug 14 15:19:36 CST 2013:新建文件夹:ENTRY_CREATE  //新建了一个文件夹
Wed Aug 14 15:19:40 CST 2013:新建文件夹:ENTRY_MODIFY //在新建的文件夹下放了一个文件
Wed Aug 14 15:19:46 CST 2013:新建文件夹:ENTRY_MODIFY
Wed Aug 14 15:19:55 CST 2013:cc.txt:ENTRY_DELETE //重命名cc.txt为xx.txt
Wed Aug 14 15:19:55 CST 2013:xx.txt:ENTRY_CREATE
Wed Aug 14 15:19:55 CST 2013:xx.txt:ENTRY_MODIFY //以下为修改文件内容
Wed Aug 14 15:20:09 CST 2013:xx.txt:ENTRY_MODIFY 
Wed Aug 14 15:20:32 CST 2013:test.txt:ENTRY_MODIFY
Wed Aug 14 15:20:32 CST 2013:test.txt:ENTRY_MODIFY