0

我正在使用 commons-io jar 监视目录中的文件创建和修改。我能够在 Eclipse 控制台中获得结果。

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();

问题是当它调用 onFileCreate 和 onFileChange 方法时我无法返回文件名。如何实现这一点?而且我还试图在 onFileCreate 和 onFileChange 中调用一种方法,它返回一个列表。如何返回列表?因为在这个监听器中我没有看到除了 void 的返回参数。

//调用新方法

public String getNewMethod(File newfile) throws IOException{

    System.out.println("getList method called : "+newfile.getCanonicalPath());
     return "redirect:finalPage"; // here the redirection is not happening

}

当我看到文件更改/创建事件被触发时,我需要在 jsp 中更新更改。如何实现这一点?

4

2 回答 2

0

您可以在创建侦听器之前创建 File 对象并在 onFileCreate() 方法中设置文件。您还可以使用 OuterClass.this.OuterClassMethod() 调用匿名类方法中的方法。

File file;
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
this.file = file;                
System.out.println("File created: "
                        + file.getCanonicalPath());
OuterClassName.this.newMethod();

            } catch (IOException e) {
                e.printStackTrace(System.err);
            }

        }
        @Override
        public void onFileChange(File file) {
            try {

                System.out.println("File modified: "
                        + file.getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace(System.err);
            }

        }

    };
public list newMethod(){
// returns list here
List list = new ArrayList();
return list;
}

注意:将 OuterClassName 替换为实际的外部类名称。

于 2014-12-09T06:50:40.043 回答
0
import java.io.File;


public class Test {

    public void testMonitoring {

        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

           private String filePath;

           @Override
           public void onFileCreate(File file) {
               try {
                   // "file" is the reference to the newly created file
                   System.out.println("File created: "
                           + file.getCanonicalPath());

                   setFilePath(file.getCanonicalPath());


               } catch (IOException e) {
                   e.printStackTrace(System.err);
               }

           }
           @Override
           public void onFileChange(File file) {
               try {

                   System.out.println("File modified: "
                           + file.getCanonicalPath());
               } catch (IOException e) {
                   e.printStackTrace(System.err);
               }

           }
        /**
         * @return the filePath
         */
        public String getFilePath() {
            return filePath;
        }
        /**
         * @param filePath the filePath to set
         */
        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

       };
       observer.addListener(listener);

       //getting the file path
       System.out.println(listener.getFilePath());

       monitor.addObserver(observer);
       monitor.start();
    }

}
于 2014-12-09T06:52:18.827 回答