另一种方法可能是 - 您尝试创建一个您想要的文件夹,如果已经存在,请不要创建它,否则创建它。要实现它,您不必遍历现有文件夹。我对我的一个项目有相同的要求,我必须将内容从共享驱动器移动到 FileNet P8 并为它们分配属性,同时保持相同的文件夹路径。以下片段可能会帮助某人:
public static Folder createFolderStructure() {
Connection conn = Factory.Connection.getConnection(ConfigInfo.CE_URI);
Domain dom = Factory.Domain.getInstance(conn, null);
ObjectStore os = Factory.ObjectStore.getInstance(dom,
ConfigInfo.OBJECT_STORE_NAME);
Folder folder = null;
// pass your desired folder string here, excluding the starting root "/"
String folderpath = "APITest/some/folder/2014/12/2";
System.out.println("\nGiven input folder path is :: " + folderpath + "\n");
String[] myarray = folderpath.split("/");
for (int x = 0; x < myarray.length; x++) {
try {
if (x == 0) {
folder = Factory.Folder.createInstance(os, null);
Folder rootFolder = Factory.Folder.getInstance(os, null, "/");
folder.set_Parent(rootFolder);
folder.set_FolderName(myarray[x]);
System.out.println("Creating main (first) folder.. \t" + myarray[x]);
folder.save(RefreshMode.NO_REFRESH);
} else {
String currentfolder = myarray[x];
String parentfolder = "";
for (int i = 0; i < x; i++) {
folder = Factory.Folder.createInstance(os, null);
parentfolder = parentfolder + "/" + myarray[i];
Folder nxtrootFolder = Factory.Folder.getInstance(os, null, parentfolder);
folder.set_Parent(nxtrootFolder);
folder.set_FolderName(currentfolder);
}
System.out
.println("Trying to create " + currentfolder + " in " + parentfolder);
folder.save(RefreshMode.NO_REFRESH);
}
} catch (EngineRuntimeException ere) {
ExceptionCode code = ere.getExceptionCode();
if (code != ExceptionCode.E_NOT_UNIQUE) {
throw ere;
}
System.out.println("Above folder already exists...skipping...");
}
}
return folder;
}
这是从我自己的博客中复制的。