Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #5367

Re: a program process a constantly updated file

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!postnews.google.com!j25g2000vbr.googlegroups.com!not-for-mail
From www <xsli2@yahoo.com>
Newsgroups comp.lang.java.programmer
Subject Re: a program process a constantly updated file
Date Sat, 18 Jun 2011 17:40:38 -0700 (PDT)
Organization http://groups.google.com
Lines 76
Message-ID <30974ca8-32cf-43bd-afd6-9c0528882454@j25g2000vbr.googlegroups.com> (permalink)
References <885b80e6-864a-4076-b559-65b2452ffe9f@ct4g2000vbb.googlegroups.com> <itg3ts$rd7$1@dont-email.me> <itg4si$3dt$1@dont-email.me>
NNTP-Posting-Host 68.227.203.52
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1308444038 24939 127.0.0.1 (19 Jun 2011 00:40:38 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Sun, 19 Jun 2011 00:40:38 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info j25g2000vbr.googlegroups.com; posting-host=68.227.203.52; posting-account=oo44OQoAAABMDvaRlaO3SeQ5MVbxqLdH
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-Header-Order ARLUEHNKC
X-HTTP-UserAgent Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; HPNTDF; BRI/1; .NET4.0C),gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5367

Show key headers only | View raw


Thank you very much.

Based on what I found from google, I have made the following code. It
is very short and works well. It needs two extra apache jar files
which are available from web:
commons-vfs-1.0.jar
commons-logging-1.1.1.jar

<<FileMonitor.java>>
package test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;

public class FileMonitor implements FileListener
{


    public static void main(final String[] args) throws Exception
    {

        final String inputFileName = "C:\\Program Files (x86)\\temp\
\file_to_be_monitored.txt";

        try
        {
            final FileSystemManager fsManager = VFS.getManager();
            final FileObject listendir =
fsManager.resolveFile(inputFileName);

            final DefaultFileMonitor fm = new DefaultFileMonitor(new
FileMonitor());
            fm.setRecursive(true);
            fm.addFile(listendir);
            fm.start();
            fm.run();
        }
        catch(final FileSystemException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void fileChanged(final FileChangeEvent fe) throws Exception
    {
        System.out.println("file changed.");
    }

    public void fileCreated(final FileChangeEvent fe) throws Exception
    {
        System.out.println("file created");
        //// what ever you need to just write here

    }

    public void fileDeleted(final FileChangeEvent fe) throws Exception
    {
        System.out.println("file deleted");
    }
}
</End FileMonitor.java>

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

a program process a constantly updated file www <xsli2@yahoo.com> - 2011-06-17 10:15 -0700
  Re: a program process a constantly updated file www <xsli2@yahoo.com> - 2011-06-17 10:43 -0700
  Re: a program process a constantly updated file Jeff Higgins <jeff@invalid.invalid> - 2011-06-17 13:51 -0400
    Re: a program process a constantly updated file Jeff Higgins <jeff@invalid.invalid> - 2011-06-17 14:07 -0400
      Re: a program process a constantly updated file www <xsli2@yahoo.com> - 2011-06-18 17:40 -0700
  Re: a program process a constantly updated file Roedy Green <see_website@mindprod.com.invalid> - 2011-06-19 11:12 -0700

csiph-web