I need to write a custom batch File renamer. I've got the bulk of it done except I can't figure out how to check if a file is already open. I'm just using the java.io.File
package and there is a canWrite()
method but that doesn't seem to test if the file is in use by another program. Any ideas on how I can make this work?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I don't think you'll ever get a definitive solution for this, the operating system isn't necessarily going to tell you if the file is open or not.
You might get some mileage out of
java.nio.channels.FileLock
, although the javadoc is loaded with caveats.If file is in use
FileOutputStream fileOutputStream = new FileOutputStream(file);
returns java.io.FileNotFoundException with 'The process cannot access the file because it is being used by another process' in the exception message.