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){}
}
}
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