Locking and unlocking files using the java API

前端 未结 2 2044
孤独总比滥情好
孤独总比滥情好 2021-01-19 06:09

One of our clients is using some Novel security software that sometimes locks some .class files that our software creates. This causes some nasty problems for them when thi

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-19 06:12

    File.canWrite() has the race condition that Asaph mentioned. You could try FileChannel.lock() and get an exclusive lock on the file. As long as the .class is on your local disk, this should work fine (file locking can be problematic on networked disks).

    Alternatively, depending on how the .class name is discovered, you could create a new name for your .class each time; then if the anti-virus software locks your initial class, you can still create the new one.

提交回复
热议问题