文件锁

File file = new File("d:"+File.separator+" test.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()+ "文件已加锁");
			Thread.sleep(10000);
			lock.release();
			System.out.println(file.getName()+"文件锁已释放");
		
		}
		fout.close();
		output.close();

你可能感兴趣的:(文件锁)