Force IOException during file reading

Disclaimer: I have not tested this on a non-Windows platform, so it may have different results on a platform with different file locking characteristics.

If you lock the file beforehand, you can trigger an IOException when something attempts to read from it:

java.io.IOException: The process cannot access the file because another process has locked a portion of the file

This works even if you are in the same thread.

Here’s some sample code:

final RandomAccessFile raFile = new RandomAccessFile(csvFile, "rw");
raFile.getChannel().lock();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)