Wednesday, August 8, 2012

File Watcher Program in Java

Java Program to create a class called File Watcher that can be given several file names
that may or may not exist. the class should start a thread for each file name
Each thread will periodically check for the existence of it's file.
If the file exist,the thread will write a message to the console & then end.

import java.io.*;
class SlipFileWatch
{
 public static void main(String args[]) throws IOException
  {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\n Enter Several N  files");
        try
        {
          int n =Integer.parseInt(br.readLine());
          System.out.println("\n Enter File name");
          for(int i=1;i<=n;i++)
                  new FileWatcher(br.readLine());
        }  
        catch(Exception e)
        { System.out.println("Invalid Input");}
    }
}   
   
class FileWatcher implements Runnable
{
    Thread T;
    String fname;
    FileWatcher(String fn)
    {
        fname=fn;
        T=new Thread(this,fname);
        System.out.println("\n\nNew = "+T);
        T.start();
    }
   
    public void run()
    {
        File f = new File(fname);
        if(f.exists())
          System.out.println(" "+fname+"  File Exist ");
        else 
          System.out.println(" "+fname+"  File Not Exist");
         try
          {
              Thread.sleep(1000);
          }
          catch(InterruptedException it){}
    }
}

No comments:

Post a Comment

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...