Java FileVisitor访问在目录中排序

时间:2016-03-17 19:12:15

标签: java java-stream filevisitor directorystream

我正在尝试使用Java的FileVisitor接口来遍历文件列表并将内容导入数据库。方案是按顺序导入csv内容。可能有一个名为Object.csv,Object_updated.csv,Object_deleted.csv的文件,我需要将内容合并到一个数据库中。 文件夹层次结构将包含:

basepath

        folderid

                Objects

                       objectname

                                 objectname.csv

                                 objectname_updated.csv

                                 objectname_deleted.csv

        folderid_2 ....

这是我提出的基本解析:

 private void testMethod() throws Exception
    {
        Storage.dumpPath = "C:\\Users\\TestUser\\Documents\\RestoreDummy";

        Files.list(Paths.get(Storage.dumpPath)).sorted().forEach(path ->
        {
            logger.info("Main dir: " + path);
            try
            {
                Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                    boolean objectDir = false;

                    @Override
                    public FileVisitResult preVisitDirectory(Path paramT, BasicFileAttributes paramBasicFileAttributes)
                            throws IOException
                    {
                        logger.info("Previsit dir: " + paramT);
                        if (paramT.getParent().endsWith(Constants.objectView)
                                && paramT.getParent().getParent().equals(path))
                            objectDir = true;
                        else
                            objectDir = false;
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFile(Path mypath, BasicFileAttributes paramBasicFileAttributes)
                            throws IOException
                    {
                        if (objectDir && mypath.toString().endsWith("csv"))
                        {
                            logger.info("Visiting: " + mypath);
                        }
                        return FileVisitResult.CONTINUE;
                    }
                });
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        });
    }

这返回了一个文件列表:

Main dir: C:\Users\TestUser\Documents\RestoreDummy\1

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\1\Objects

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\1\Objects\ObjectName

Visiting: C:\Users\TestUser\Documents\RestoreDummy\1\Objects\ObjectName\ObjectName.csv

Main dir: C:\Users\TestUser\Documents\RestoreDummy\2

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\2

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\2\Describes

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\2\Objects

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\2\Objects\ObjectName

Visiting: C:\Users\TestUser\Documents\RestoreDummy\2\Objects\ObjectName\ObjectName_updated.csv

Main dir: C:\Users\TestUser\Documents\RestoreDummy\3

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\3

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\3\Describes

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\3\Objects

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\3\Objects\ObjectName

Visiting: C:\Users\TestUser\Documents\RestoreDummy\3\Objects\ObjectName\ObjectName_deleted.csv

Visiting: C:\Users\TestUser\Documents\RestoreDummy\3\Objects\ObjectName\ObjectName_updated.csv

Main dir: C:\Users\TestUser\Documents\RestoreDummy\4

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\4

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\4\Describes

Main dir: C:\Users\TestUser\Documents\RestoreDummy\5

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\5

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\5\Describes

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\5\Objects

Main dir: C:\Users\TestUser\Documents\RestoreDummy\6

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\6

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\6\Describes

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\6\Objects

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\6\Objects\ObjectName

Visiting: C:\Users\TestUser\Documents\RestoreDummy\6\Objects\ObjectName\ObjectName_updated.csv

Main dir: C:\Users\TestUser\Documents\RestoreDummy\7

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\7

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\7\Describes

Previsit dir: C:\Users\TestUser\Documents\RestoreDummy\7\Objects

如果有两个文件objectname_updated.csv和objectname_deleted.csv,则应首先遍历objectname_updated.csv而不是objectname_deleted.csv。这个特定的实例正在运行,但文档说文件列表不保证是有序的。有没有办法根据文件名获取排序,还是我必须存储文件列表预分类?

1 个答案:

答案 0 :(得分:0)

preVisitDirectory()中,您可以获取所有文件并对其进行排序并进行处理。

然后跳过访问子树。

这样的事情:

private static final List<Path> PATH_ORDER = Arrays.asList(Paths.get("objectname.csv"),
   Paths.get("objectname_updated.csv"),
   Paths.get("objectname_deleted.csv"));

@Override
public FileVisitResult preVisitDirectory(Path paramT, BasicFileAttributes paramBasicFileAttributes)
        throws IOException
{
    logger.info("Previsit dir: " + paramT);
    if (paramT.getParent().endsWith(Constants.objectView)
            && paramT.getParent().getParent().equals(path))
    {
      try(Stream<Path> children = Files.list(paramT))
      {
        Path[] sortedPaths = children
            .filter(path -> path.endsWith(".csv"))
            .sorted((x, y) -> 
                   PATH_ORDER.indexOf(x.getFileName())
                   -
                   PATH_ORDER.indexOf(y.getFileName()))
           .toArray(Path[]::new);

        // Process them
      }

      return FileVisitResult.SKIP_SUBTREE;
    }

    return FileVisitResult.CONTINUE;
}                    

注意:您也可以尝试使用Guava Ordering进行排序