NIO使用小结

1、FileLock文件锁
import java.io.File ;
import java.io.FileOutputStream ;
import java.nio.channels.FileChannel ;
import java.nio.channels.FileLock ;

public class FileLockDemo{
  public static void main(String args[]) throws Exception {
    File file = new File("d:" + File.separator + "demo01.txt") ;
    FileOutputStream output = null ;
    output = new FileOutputStream(file,true) ;
    FileChannel fout = null ;
    fout = output.getChannel() ;// 得到通道
    FileLock lock = fout.tryLock() ; // 进行独占锁的操作
    if(lock!=null){
      System.out.println(file.getName() + "文件锁定300秒") ;
      Thread.sleep(300000) ;
      lock.release() ;  // 释放
      System.out.println(file.getName() + "文件解除锁定。") ;
    }
    fout.close() ;
    output.close() ;
  }
}
2、NIO中的Selector类使用
 
  1. import java.net.InetSocketAddress ; 
  2. import java.net.ServerSocket ; 
  3. import java.util.Set ; 
  4. import java.util.Iterator ; 
  5. import java.util.Date ; 
  6. import java.nio.channels.ServerSocketChannel ; 
  7. import java.nio.ByteBuffer ; 
  8. import java.nio.channels.SocketChannel ; 
  9. import java.nio.channels.Selector  ; 
  10. import java.nio.channels.SelectionKey  ; 
  11. public class DateServer{ 
  12.     public static void main(String args[]) throws Exception { 
  13.         int ports[] = {8000,8001,8002,8003,8005,8006} ; // 表示五个监听端口 
  14.         Selector selector = Selector.open() ;   // 通过open()方法找到Selector 
  15.         for(int i=0;i<ports.length;i++){ 
  16.             ServerSocketChannel initSer = null ; 
  17.             initSer = ServerSocketChannel.open() ;  // 打开服务器的通道 
  18.             initSer.configureBlocking(false) ;  // 服务器配置为非阻塞 
  19.             ServerSocket initSock = initSer.socket() ; 
  20.             InetSocketAddress address = null ; 
  21.             address = new InetSocketAddress(ports[i]) ; // 实例化绑定地址 
  22.             initSock.bind(address) ;    // 进行服务的绑定 
  23.             initSer.register(selector,SelectionKey.OP_ACCEPT) ; // 等待连接 
  24.             System.out.println("服务器运行,在" + ports[i] + "端口监听。") ; 
  25.         } 
  26.         // 要接收全部生成的key,并通过连接进行判断是否获取客户端的输出 
  27.         int keysAdd = 0 ; 
  28.         while((keysAdd=selector.select())>0){   // 选择一组键,并且相应的通道已经准备就绪 
  29.             Set<SelectionKey> selectedKeys = selector.selectedKeys() ;// 取出全部生成的key 
  30.             Iterator<SelectionKey> iter = selectedKeys.iterator() ; 
  31.             while(iter.hasNext()){ 
  32.                 SelectionKey key = iter.next() ;    // 取出每一个key 
  33.                 if(key.isAcceptable()){ 
  34.                     ServerSocketChannel server = (ServerSocketChannel)key.channel() ; 
  35.                     SocketChannel client = server.accept() ;    // 接收新连接 
  36.                     client.configureBlocking(false) ;// 配置为非阻塞 
  37.                     ByteBuffer outBuf = ByteBuffer.allocateDirect(1024) ;   // 
  38.                     outBuf.put(("当前的时间为:" + new Date()).getBytes()) ;   // 向缓冲区中设置内容 
  39.                     outBuf.flip() ; 
  40.                     client.write(outBuf) ;  // 输出内容 
  41.                     client.close() ;    // 关闭 
  42.                 } 
  43.             } 
  44.             selectedKeys.clear() ;  // 清楚全部的key 
  45.         } 
  46.          
  47.     }