This code snippet will continuoudly monitor a specified file. If any text file is pasted in the folder it will fetch the file, reads the file and keep the backup in the specified folder. And delets the file from the current folder.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /*Monitors the file using threads *input: File paths *Output: reading the file, copying to some folder, deleting the file */ public class MonitorThread implements Runnable { public static String outFileName = "/Users/raj/Downloads/temp/output/"; public static String sourceDirStream = "/Users/raj/Downloads/temp/target/"; private volatile boolean keepRunning = true; Thread t; private String interval; private boolean isExecutingForTheFirstTime = true; public MonitorThread(){ t = new Thread(this,"1"); t.start(); } public void run() { while (keepRunning) { try { int min =0; int sleepTime = 0; int hour = 0; if(keepRunning) { if(this.isExecutingForTheFirstTime) { min =get_intervalmins(); hour =get_intervalhour(); sleepTime = (((hour*60)+min)+1)-((hour*60)+min); Thread.sleep(sleepTime*60000); isExecutingForTheFirstTime = false; // first time only needed } MonitorStream(); min =get_intervalmins(); hour =get_intervalhour(); sleepTime = (((hour*60)+min)+1)-((hour*60)+min); Thread.sleep(sleepTime*60000); } } catch(Exception ex) { ex.printStackTrace(); keepRunning = false; } } } //Functions to get the currentHour and Minutes from System clock. private int get_intervalhour() { Calendar c = Calendar.getInstance(); int mins = c.get(Calendar.MINUTE); int hrs = c.get(Calendar.HOUR_OF_DAY); return(hrs); } private int get_intervalmins() { Calendar c = Calendar.getInstance(); int mins = c.get(Calendar.MINUTE); int hrs = c.get(Calendar.HOUR_OF_DAY); return(mins); } private int get_intervalsecs() { Calendar c = Calendar.getInstance(); int secs = c.get(Calendar.SECOND); return(secs); } private String get_interval(int sleepinterval) { Calendar c = Calendar.getInstance(); int mins = c.get(Calendar.MINUTE); int hrs = c.get(Calendar.HOUR_OF_DAY); String strt_interval,end_interval; if(sleepinterval == 30) { if (mins > 15 && mins<=30) { strt_interval = hrs+":00"; end_interval =hrs+":15"; interval =strt_interval+"-"+end_interval; } if (mins <= 15 && mins != 0 && mins>0) { int hr = hrs-1; if(hr == -1) { hr= 23; } strt_interval = hr+":45"; end_interval = hrs+":00"; interval=strt_interval+"-"+end_interval; } if(mins ==0) { int hr = hrs-1; if(hr == -1) { hr= 23; } strt_interval = hr+":30"; end_interval = hr+":45"; interval=strt_interval+"-"+end_interval; } if(mins>30 && mins<=45) { strt_interval = hrs+":15"; end_interval = hrs+":30"; interval=strt_interval+"-"+end_interval; } } if(sleepinterval ==60) { if (mins > 30 ) { strt_interval = hrs+":00"; end_interval =hrs+":30"; interval =strt_interval+"-"+end_interval; } if (mins <= 30 && mins != 0) { int hr = hrs-1; if(hr == -1) { hr= 23; } strt_interval = hr+":30"; end_interval = hrs+":00"; interval=strt_interval+"-"+end_interval; } if(mins ==0) { int hr = hrs-1; if(hr == -1) { hr= 23; } strt_interval = hr+":00"; end_interval = hr+":30"; interval=strt_interval+"-"+end_interval; } } if(sleepinterval ==1440) { c.add(Calendar.DATE,-1); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); interval = "report for the date " + sdf.format( c.getTime()); } return(interval); } public synchronized void MonitorStream() { try { File localFile = new File(sourceDirStream); String[] Stream = localFile.list(); for(int ii=0; ii< Stream.length ; ii++) { try{ System.out.println("file Path = "+sourceDirStream+Stream[ii]); displayAndReplaceFile(Stream[ii]); deletefile(Stream[ii]); }catch(Exception e){ System.out.println(e); } } } catch (Exception ftpe) { ftpe.printStackTrace(); } } private static void deletefile(String file){ File f1 = new File(sourceDirStream+file); boolean success = f1.delete(); if (!success){ System.out.println(file+" Deletion failed."); }else{ System.out.println(file+" File deleted."); } } public static void main(String[] args){ try{ new MonitorThread(); } catch(Exception e) { System.out.println("error "+e); } } public void displayAndReplaceFile(String fileName) { BufferedReader buffInputFile = null; String dataRowFrmFile = null; String outFile = getDate() + fileName; String inpFile = sourceDirStream + fileName; System.out.println("File path is : " + inpFile); try { buffInputFile = new BufferedReader(new FileReader(inpFile)); int count = 0; FileWriter fstream = new FileWriter(outFileName+outFile); BufferedWriter out1 = new BufferedWriter(fstream); System.out.println(" outFileName " + outFileName); while ((dataRowFrmFile = buffInputFile.readLine()) != null) { out1.write(dataRowFrmFile); out1.newLine(); if (count == 0) { System.out.println(dataRowFrmFile); } else { System.out.println(dataRowFrmFile); } count++; } buffInputFile.close(); buffInputFile = null; } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } private static String getDate() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HHmmss_"); return sdf.format(new Date()); } }