-
File-system magic?
Folks, I have a conceptual problem that I can use some help on.
Lets say I have an application that stores any kind of data file in
a database. Said database entries would be represented visually
to an end user. When double-clicking on any such entry, the will
be downloaded to some temporary folder and 'executed' (i.e.
whatever program is associated with the file will launch and
load it).
Would there be any reasonable way to check if a user has
finished with the file, and if so if the file was changed (so it
could be uploaded back to the db server as 'modified')?
I considered timers and loops to do the job, but this sucks
away alot of processing time unnecessarily. I also considered low
level file system hooking (platform dependent code), but that
defeats the benefits of java.
So, any ideas / advice?
-CC
-
You could use lastModified() which returns a long value which represents the time the file was modified.
-
Thanks Dilinger,
Would there be any way to reactively use this function to some
event or would you forsee this as being a polling implementation?
-CC
-
Polling implementation for sure.
-
Darn, I had a feeling you were gonna' say that. Thanks for your
input regardless!
-
How far back do you want to check to see when the file was modified? Im not sure how accurate this is or my math for that matter. :lol:
Code:
import java.io.*;
import java.util.Date;
public class filetest{
public static void main(String[] args){
File f = new File("C:" + File.separator + "Test.txt");
long currenttime = System.currentTimeMillis();
long lastmodified = f.lastModified();
System.out.println(new Date(currenttime));
System.out.println(new Date(lastmodified));
howlongagomodified(currenttime, lastmodified);
}
public static void howlongagomodified(long currenttime, long lastmodified){
//how long ago was the file modified?
long l = currenttime - lastmodified;
long secspast = l / 1000;
long minspast = secspast / 60;
long hourspast = minspast / 60;
System.out.println("File was modified: " + (minspast / 60) + " hours "
+ (minspast % 60) +
" minutes ago");
}
}
-
How do you execute the editing app, anyway? You can use the Process object returned by Runtime.exec to check when the app exits. Then you check if the lastModified time has changed.
That's what I'm doing, anyway.
-
Dilenger4,
Thanks for the code. Alghough it doesn't seem to address my
main concern (an automatic trigger for when a temp file has
been modified), it is certainly a nice starter.
CornedBee,
I actually have not chosen an implementation yet. I would
probably just 'execute the file' and see what the OS did with
it (ex. in Windows the natural association may link it up
with the appropriate app).
I guess an alternative idea may be to keep track of the application
association within MY application and then use the process
object to launch the app with the file being a parameter. Perhaps
I could then respond to the termination of the process with some
kind of exception handler that would in turn check the file data
and re-upload the file to the database if it had changed. What
do you think?
-
I know that using Runtime.exec fails if the file is not a real executable. You can't exec data files.
I wrote a JNI class that executes the stuff. The computer it is on is currently out of order though.
-
Darn... well it looks like I have another problem to push on the
stack. I do however want to stay away from JNI in this case, but
I think it's cool that you found a JNI solution.
-
I can give you my JNI stuff once I get it back. Works on Windows, but I need to write a Linux implementation.
-