I have a java code that monitors files in a folder, the files are dropped by ftp from another system via a table. How do i ensure that i do not pick up incomplete files? The files dropped are XML files.
Asked
Active
Viewed 54 times
-1
-
By incomplete to you mean not fully copied yet? – dan b Dec 18 '14 at 08:55
-
I would get the copying system to do a rename to a certain extension when completely copied - if possible. – Scary Wombat Dec 18 '14 at 08:56
1 Answers
0
Try to lock the file. If it is not complete, you can not lock it:
private boolean isFileComplete(File file) {
FileLock lock = null;
FileChannel channel = null;
try {
channel = new RandomAccessFile(file, "rw").getChannel();
lock = channel.lock();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (lock != null)
try {
lock.release();
channel.close();
} catch (IOException e) {
//Keine Behandlung Notwendig
}
}
return true;
}
If it is lockable, the file is complete.

Jens
- 67,715
- 15
- 98
- 113