繁体   English   中英

在文件夹中创建新文件时是否可以运行循环?

[英]Is it possible to run a loop when a new file is created in a folder?

因此,我必须用Java编写一个程序,该程序会在后台自动运行,并寻找一个新的.dat文件,当它看到新的.dat文件时,便会运行一个.bat文件以将数据加载到数据库中。 到目前为止,我有一个程序可以监视新文件的创建,修改和删除。 我也有一个运行.bat文件并将数据加载到数据库中的脚本,现在我只需要连接两者即可,但是我不确定该怎么做,如果有人可以指出正确的方向,我将不胜感激它。

下面是我到目前为止的代码。

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

import java.io.*;
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;


public class Order_Processing {

    public static void watchDirectoryPath(Path path)
    {
        try {
            Boolean isFolder = (Boolean) Files.getAttribute(path,
                    "basic:isDirectory", NOFOLLOW_LINKS);
            if (!isFolder)
            {
                throw new IllegalArgumentException("Path: " + path
                        + " is not a folder");
            }
        } 
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
        System.out.println("Watching path: "+ path);
        FileSystem fs = path.getFileSystem();
        try (WatchService service = fs.newWatchService())
        {
            path.register(service, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
            WatchKey key = null;
            while (true) 
            {
                key = service.take();

                Kind<?> kind = null;
                for (WatchEvent<?> watchEvent : key.pollEvents())
                {
                    kind = watchEvent.kind();
                    if (OVERFLOW == kind)
                    {
                        continue;
                    }
                    else if (ENTRY_CREATE == kind)
                    {
                        Path newPath = ((WatchEvent<Path>) watchEvent)
                                .context();
                        System.out.println("New Path Created: " + newPath);
                    }
                    else if (ENTRY_MODIFY == kind)
                    {
                        Path newPath = ((WatchEvent<Path>) watchEvent)
                                .context();
                        System.out.println("New path modified: "+ newPath);
                    }
                    else if (ENTRY_DELETE == kind)
                    {
                        Path newPath = ((WatchEvent<Path>) watchEvent)
                                .context();
                        System.out.println("New path deleted: "+ newPath);
                    }
                }
                if (!key.reset())
                {
                    break;
                }
            }
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        } 
        catch (InterruptedException ie)
        {
            ie.printStackTrace();
        }
    }

public static void main(String[] args)
                            throws FileNotFoundException
{
    File dir = new File("C:\\Paradigm");
    watchDirectoryPath(dir.toPath());

    //below is the script that runs the .bat file and it works if by itself
    //with out all the other watch code.
    try {
        String[] command = {"cmd.exe", "/C", "Start", "C:\\Try.bat"};
        Process p =  Runtime.getRuntime().exec(command);           
    } 
    catch (IOException ex) {
    }
    }
 }

这是行不通的,因为您有一段while (true) 这是有道理的,因为您正在听并且希望连续发生; 但是,bat调用将永远不会执行,因为watchDirectory(...)将永远不会终止。 为了解决这个问题,像这样将主体的其余部分拉出自己的功能

public static void executeBat() {
    try {
        String[] command = {"cmd.exe", "/C", "Start", "C:\\Try.bat"};
        Process p =  Runtime.getRuntime().exec(command);
    }
    catch (IOException ex) {
        // You should do something with this. 
        // DON'T JUST IGNORE FAILURES
    }

这样,在创建文件时,您可以调用该bat脚本

...
else if (ENTRY_CREATE == kind)
{
    Path newPath = ((WatchEvent<Path>) watchEvent).context();
    executeBat();
}
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM