Verificar se um arquivo txt está sendo utilizado

0 respostas
danilorangelmg

estou tendo um problema, preciso verificar se algum processo está utilizando um arquivo txt pois se existir alguem manipulando ele nao poderei trabalhar as informações contidas nele, eu tentei utilizar o FileLock de varias formas diferentes e não tive sucesso testei:

FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
FileLock lock = channel.tryLock();
boolean bloqueado = (lock == null);
if (bloqueado) {
	System.out.println("Arquivo em uso: " + file.getName());
	return;
}
lock.release();

//este tb nao deu certo

FileChannel channel = new RandomAccessFile(file, "r").getChannel();
FileLock lock = channel.lock();
try {
   lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
System.out.println("Arquivo em uso: " + file.getName());
return;
} finally {
   lock.release();
}
channel.close();

alguem sabe de alguma outra forma de verificar isso?

Criado 3 de abril de 2012
Respostas 0
Participantes 1