1

我希望我的 java 程序不断扫描特定文件夹中的新文件。
我期待类似的东西:

while(1) {

String csvFile = "D:\\myfiles\\done\\*.*";
br = new BufferedReader(new FileReader(csvFile));

 // DO Some thing

}

有没有好的方法来做到这一点?

4

5 回答 5

4

您可能不想一遍又一遍地执行循环,因为这会导致性能下降。您可以改为监视目录更改,这将在没有新文件可用时暂停工作线程(链接的文章除外):

WatchKey key;
try {
    key = watcher.take(); // blocking call, similar to sleep()
} catch (InterruptedException x) {
    return; // this happens if your thread got interrupted
}
// do something with the recived key
于 2013-09-01T10:58:17.120 回答
2

您可以使用File来获取所有文件。

检查这个:

File folder = new File("D:/myfiles/done");
File[] files = folder.listFiles();
for(File f : files){
    // Read f in here
}
于 2013-09-01T10:55:06.777 回答
1

这个FileObserver类看起来很有前途:

/**
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://opensso.dev.java.net/public/CDDLv1.0.html or
 * opensso/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at opensso/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * $Id: FileObserver.java,v 1.3 2008/06/25 05:44:08 qcheng Exp $
 *
 */
package com.sun.identity.sm.flatfile;

import com.iplanet.am.util.SystemProperties;
import com.sun.identity.shared.Constants;
import com.sun.identity.shared.debug.Debug;
import com.sun.identity.sm.SMSObjectListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

class FileObserver extends Thread {
    private static Debug debug = Debug.getInstance("amSMSEvent");
    private Map snapShot;
    private int interval;
    private boolean running;
    private FlatFileEventManager eventManager;

    FileObserver(FlatFileEventManager eventManager) {
        setDaemon(true);
        getPollingInterval();
        this.eventManager = eventManager;
    }

    private void getPollingInterval() {
        String time = SystemProperties.get(
            Constants.CACHE_POLLING_TIME_PROPERTY);
        interval = Constants.DEFAULT_CACHE_POLLING_TIME;
        if (time != null) {
            try {
                interval = Integer.parseInt(time); 
            } catch (NumberFormatException nfe) {
                debug.error(
                    "FileObserver.getCachePollingInterval", nfe);
            }
        }
        interval = interval * 60 * 1000;
    }

    /**
     * Returns <code>true</code> if thread is running.
     *
     * @return <code>true</code> if thread is running.
     */
    public boolean isRunning() {
        return running;
    }

    /**
     * Stops the thread.
     */
    public void stopThread() {
        running = false;
    }

    public void run() {
        running = true;
        snapShot = getCurrentSnapShot();
        try {
            while (running) {
                /*
                 * This flag set to false in the begin of the thread.
                 * when a node is added/delete from the file system, we need
                 * to toggle this flag which in turn ask the 
                 * SMSEnhancedFlatFileObject to rebuild the directory tree.
                 */
                boolean needReloadRootNode = false;
                sleep(interval);
                Map newSnapShot = getCurrentSnapShot();

                if (snapShot != null) {
                    for (Iterator i = newSnapShot.keySet().iterator();
                        i.hasNext();
                    ) {
                        String filename = (String)i.next();
                        if (snapShot.containsKey(filename)) {
                            long prev =((Long)snapShot.get(filename))
                                .longValue();
                            long curr =((Long)newSnapShot.get(filename))
                                .longValue();
                            if (prev != curr) {
                                eventManager.notify(getDN(filename),
                                    SMSObjectListener.MODIFY);
                            }
                        } else {
                            if (!needReloadRootNode) {
                                eventManager.reloadRootNode();
                                needReloadRootNode = true;
                            }
                            eventManager.notify(getDN(filename),
                                SMSObjectListener.ADD);
                        }
                    }

                    for (Iterator i = snapShot.keySet().iterator();
                        i.hasNext();
                    ) {
                        String filename = (String)i.next();

                        if (!newSnapShot.containsKey(filename)) {
                            if (!needReloadRootNode) {
                                eventManager.reloadRootNode();
                                needReloadRootNode = true;
                            }
                            eventManager.notify(getDN(filename),
                                SMSObjectListener.DELETE);
                        }
                    }
                }
                snapShot = newSnapShot;
            }
        } catch (InterruptedException e) {
            debug.warning("FileObserver.run", e);
        }
    }

    private String getDN(String filename) {
        BufferedReader buff = null;
        String dn = null;

        try{
            buff = new BufferedReader(new FileReader(filename));
            String line = buff.readLine();
            if ((line != null) && line.startsWith("#")) {
                dn = line.substring(1);
            }
        } catch (IOException e) {
            debug.warning("FileObserver.getDN", e);
        } finally {
            if (buff != null) {
                try {
                    buff.close();
                } catch (IOException ex) {
                    //ignored
                }
            }
        }
        return dn;
    }

    private Map getCurrentSnapShot() {
        Map snapshot = null;
        String baseDir = SystemProperties.get(
            SMSFlatFileObjectBase.SMS_FLATFILE_ROOTDIR_PROPERTY);
        File dir = new File(baseDir);
        String[] files = dir.list();

        // if the war is not configured we may not get any files here.
        if (files.length > 0) {
            snapshot = new HashMap(files.length *2);
            for (int i = 0; i < files.length; i++) {
                String filename = baseDir + "/" + files[i];
                File f = new File(filename);
                if (!f.isDirectory()) {
                    snapshot.put(filename, new Long(f.lastModified()));
                }
            }
        }
        return snapshot;
    }
}

如果您更喜欢库,还可以查看 Apache commons FileAlterationObserver

于 2013-09-01T10:57:06.237 回答
0

这可能很有用。

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class DirectoryWatchDemo {

    public static void main(String[] args) {
        try {
            WatchService watcher = FileSystems.getDefault().newWatchService();
            Path dir = Paths.get("D:\\Mani_Test\\");
            dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
            System.out.println("Watch Service registered for dir: " + dir.getFileName());
            while (true) {
                WatchKey key;
                try {
                    key = watcher.take();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                    return;
                }
                for (WatchEvent<?> event : key.pollEvents()) {
                    WatchEvent.Kind<?> kind = event.kind();
                    WatchEvent<Path> ev = (WatchEvent<Path>) event;
                    Path fileName = ev.context();
                    System.out.println(kind.name() + ": " + fileName);
                }
                boolean valid = key.reset();
                if (!valid) {
                    break;
                }
            }
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}
于 2017-05-17T06:18:35.630 回答
-1

文件对象也可以用于文件夹,这是代码

String filePath = "D:\\myfiles\\done\\";
        File dir = new File(filePath);
        if (dir.isDirectory())
        {
            File[] fileList = dir.listFiles();
            if (fileList.length > 0)
            {
                // Your code here
            }
        }
于 2013-09-01T10:58:02.123 回答