锁住程序,只允许一个程序运行 FileLock
http://www.cnblogs.com/DreamDrive/p/7425281.html
Demo
private static void makeSingle(){ RandomAccessFile raf=null; FileChannel channel=null; FileLock lock=null; try{ File sf=new File("aaaa.single"); sf.deleteOnExit(); sf.createNewFile(); raf=new RandomAccessFile(sf,"rw"); channel=raf.getChannel(); lock=channel.tryLock(); }catch(Exception e){ e.printStackTrace(); } } if(lock==null){ system.exit(0);//正常退出程序 }