I am monitoring the file creation and modification in directory using commons-io jar.I am able to get the results in Eclipse console.
final long pollingInterval = 5 * 1000;
String FOLDER = "C:/test";
File folder = new File(FOLDER);
folder.setReadable(true);
if (!folder.exists()) {
// Test to see if monitored folder exists
throw new RuntimeException("Directory not found: " + FOLDER);
}
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor =
new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
// Is triggered when a file is created in the monitored folder
@Override
public void onFileCreate(File file) {
try {
// "file" is the reference to the newly created file
System.out.println("File created: "
+ file.getCanonicalPath());
getNewMethod(file);// here in this method i am not able to return since its void.
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
@Override
public void onFileChange(File file) {
try {
System.out.println("File modified: "
+ file.getCanonicalPath());
getNewMethod(file); // here in this method i am not able to return since its void.
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();
The problem is that i cannot able to return the file name when it is calling onFileCreate and onFileChange methods.How to achieve this? And also i am trying to call one method inside onFileCreate and onFileChange which returns a list.How to return the list? Because in this listener i dont see return parameter except void.
//Calling a newMethod
public String getNewMethod(File newfile) throws IOException{
System.out.println("getList method called : "+newfile.getCanonicalPath());
return "redirect:finalPage"; // here the redirection is not happening
}
When i see file change/create event is triggered, i need the changes to be update in jsp.How to achieve this?
You can create File object before you create listener and set file in onFileCreate() method. You can also call the methods within anonymous class methods by using OuterClass.this.OuterClassMethod().
Note: Replace OuterClassName with actual outer class name.