Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Watch for file edit

Posted on 2005-04-21
11
Medium Priority
?
269 Views
Last Modified: 2013-11-13
Hi,

I need to watch from Java, how the user is editing a file, and take an action (upload back to the web site), when he is done.

If he uses MS Office, it locks the file, so I plan to try putting a Java lock on the file until I succedd. Any details would be helpful.

if he is editing a text file, I could rename it to file.txt.doc, as a hack, and open in Word, but this is less than perfect solution.

I know that Winzip somehow watches for a file to change, and adds it back to the archive. But I am hesitant to start a watching thread - because it may then work forever. User will eventually close a Zip file, but my application stays.

Thank you.
0
Comment
Question by:kerzner
  • 7
  • 4
11 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 13840887
To achieve this the same way that WinZip does, would require the use of JNI (to implement a Native Language, such as C++). Alternatively, what I would do, would be to create an individual thread for each file. These threads will keep looping, checking the size of the file, if the file size changes, then it will perform whatever task(s) necessary..
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13840908
An example, would be:


class FileWatch extends Thread
{
   
    private File file;
    private long length = -1;
   
    public FileWatch( String filename )
    {
        this.file = new File( filename );
        start();
    }
   
    public FileWatch( File fileObject )
    {
        this.file = fileObject;
        start();
    }
   
    public void run()
    {
        for ( ;; )
        {
            if ( file.length() != this.length  &&  this.length >= 0 )
            {
                action();
            }
            try
            {
                Thread.sleep( 200 );
            } catch ( Exception e ) {}
        }
    }
   
    public void action()
    {
        // Perform your action here!
    }
   
}


You can then use it like this:

    FileWatch fw = new FileWatch( "filename.ext" );

That will instantly start a new thread, then call it's action() method, once the filesize has changed.
If you have a File object for this file already, then you can pass the file object to the constructor instead, like so:

    File file = new File( "filename.ext" );
    FileWatch fw = new FileWatch( file );


Regards;
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13840917
Sorry, you need to use this code instead (I missed something out):


class FileWatch extends Thread
{
   
    private File file;
    private long length = -1;
   
    public FileWatch( String filename )
    {
        this.file = new File( filename );
        start();
    }
   
    public FileWatch( File fileObject )
    {
        this.file = fileObject;
        start();
    }
   
    public void run()
    {
        for ( ;; )
        {
            if ( file.length() != this.length  &&  this.length >= 0 )
            {
                action();
            }
            this.length = file.length();
            try
            {
                Thread.sleep( 200 );
            } catch ( Exception e ) {}
        }
    }
   
    public void action()
    {
        // Perform your action here!
    }
   
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:kerzner
ID: 13842021
The solutions seems to rely on file size change. I need to see if

- the user has the file open and when does he release it
- even if there is no change in the size

I can use code with file locking and channels found elsewhere in this site, but I am looking for more ideas.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13842064
I'm quite sure that you cannot do this with Pure Java.  :-(
0
 
LVL 1

Author Comment

by:kerzner
ID: 13842077
No would be considered an answer, if nothing else comes up.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13842121
Ah.. perhaps this can be achieved with a bit of playing around..

See this code: http://geosoft.no/software/filemonitor/FileMonitor.java.html
That will allow you to listen for creation/deletion/alteration of a file..

Now, we can then do something for whenever the file is accessed (whether it's changed or not) if we can find out how to get the "Accessed" time property of the file..
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13842156
No.. as I suspected, you cannot get the "Accessed" time of a file without Native (see here: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10932841.html).
0
 
LVL 1

Author Comment

by:kerzner
ID: 13842253
Over there, it is a lot of code, but the idea is simple: watch for time and size. I accept that locking will check if the file is locked, and JNI is to be used if i need more (such as watch for process that started this file edit)
0
 
LVL 1

Author Comment

by:kerzner
ID: 13920994
I ended up using Java locking to watch for Office apps, for other files I would have to use Native.
Thank you.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13921007
Thanks for Grade 'A'  :-)

Best of luck with the project,
Rob.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
Introduction to Processes
Starting up a Project

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question