Folder monitor Java code prints report twice -
using answers site created small folder monitor app in java. supposed check changes specific folder , output changes text file. unfortunately prints report twice every change. problem cannot figure out first line of report comes from. please me understand doing wrong.
please find code below. removed part of code not affect q&a.
import java.io.file; import java.io.bufferedwriter; import java.io.filewriter; import java.io.ioexception; import java.util.calendar; import java.util.gregoriancalendar; import java.text.simpledateformat; import java.util.date; import org.apache.commons.io.monitor.filealterationlistener; import org.apache.commons.io.monitor.filealterationlisteneradaptor; import org.apache.commons.io.monitor.filealterationmonitor; import org.apache.commons.io.monitor.filealterationobserver; public class foldermonitor { public foldermonitor() {} //path folder monitoring public static final string folder = "d:\\watcheddir"; public static void main(string[] args) throws exception { system.out.println("monitoring started"); // monitor perform polling on folder every 5 seconds final long pollinginterval = 6 * 1000; // let's directory file object , sort files. file foldertomonitor = new file(folder); file outputfile = new file("h:\\dir_changes.txt"); if (!foldertomonitor.exists()) { // test see if monitored folder exists throw new runtimeexception("directory not found: " + folder); } filealterationobserver observer = new filealterationobserver(foldertomonitor); filealterationmonitor monitor = new filealterationmonitor(pollinginterval); filealterationlistener listener = new filealterationlisteneradaptor() { // triggered when file in monitored folder modified @override public void onfilechange(file file) { // "file" reference newly created file try {writetofile(outputfile, convertlongtodate(outputfile.lastmodified()), ("file modified: "+ file.getcanonicalpath()));} catch (ioexception e) {e.printstacktrace();} } }; observer.addlistener(listener); monitor.addobserver(observer); monitor.start(); } private static void writetofile(file filepath, string timestamp, string caughtchange) throws ioexception { filewriter filewriter = new filewriter(filepath,true); bufferedwriter bufferfilewriter = new bufferedwriter(filewriter); filewriter.append("\r" + timestamp + " - " + caughtchange + "\r"); bufferfilewriter.close(); } private static string convertlongtodate(long input) { date date = new date(input); calendar cal = new gregoriancalendar(); simpledateformat sdf = new simpledateformat("yyyy/mmm/dd hh:mm:ss z"); sdf.setcalendar(cal); cal.settime(date); return sdf.format(date); } }
the output looks this:
1454622374878 file modified: d:\watcheddir\second\inside3.txt 2016/feb/04 04:46:25 est - file modified: d:\watcheddir\second\inside3.txt
i can not figure out highlighted (bold) part comes , how rid of it. please help!
i ran same code posted , don't see output in .txt file. can please try directing output new file , see if makes difference
Comments
Post a Comment