我正在尝试使用 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。这个特定的实例正在工作,但文档说文件列表不能保证是有序的。有没有办法根据文件名获得排序,还是我必须存储预先排序的文件列表?