2

I have a large directory containing files that are modified by a seperate system at varying intervals. I am running a watcher on this directory to detect which files are modified.

I'm wondering if there is some sort of trigger that occurs when a file is accessed by the system for modification. If so, the following would apply:

Using Java, is it possible to detect which files are about to be modified and make a temporary backup before that happens?
Alternately, is it possible to compare the newly modified file against it's previous version?

In this scenario, it is impossible to make a back up of every file as the files are large and there are many of them.

Example:

I have four files:

a.xml
b.xml
c.xml
d.log

b.xml has a new section added.

Is it possible to copy the newly created section into d.log?

  • 1
    The way you're going about it. No. You could write a service of some kind which would allow process to request, modify and commit a "file", but that's not a simple task. Another approach might be to back all the files up first, then as they are modified, you could generate some form of history, but again, it becomes complicated – MadProgrammer Feb 01 '16 at 04:36

3 Answers3

3

I can think of one way which could be a possible solution to your problem. "Maintain a log file which tracks lastModified date of each files and you can verify which file has been modified by using your log file.

-- Jitendra

1

How would you know if the files are about to be modified? The system handles all of the file IO. The only way you could do that is to have the program doing the modification trigger the backup, and then make the modifications. For comparison, it depends on what you want. If you want a line-by-line comparison, that should be fairly simple to do using Java's file IO classes. If you just want to check if they are the same or not, you can use a checksum on both files.

Sid Mani
  • 369
  • 3
  • 9
1

No. you can not detect a file that will be modified. not until they come up with a highly accurate future predicting AI system.

your best approach would be to maintain a versioned backup of the the files. I would start with looking into some source code management system design considerations.

awd
  • 2,302
  • 1
  • 22
  • 29
  • That sounds interesting. Thanks for that. I'm still fairly new to programming and was wondering if there was some way to tell, while it is being modified, much like some files have a name appended by a "~". Thanks for the advice, I'll take a look! – JaffolasCage Feb 01 '16 at 04:43
  • 1
    if you have control over the modifier program it becomes fairly simple. one of major the problem with "detecting file modification externally" approach is that there would be no way to make sure you are backing up older version of the file and that it has not already been modified. – awd Feb 01 '16 at 04:57
  • Even if you know that your program creates a temp file (with a ~ or such), in some cases your file would already have been modified before you can detect it and make a backup. – awd Feb 01 '16 at 05:05