2016-07-05 33 views
0

我是新來的露天。我需要將文件夾循環到各個文件夾中包含的子文件夾和文件,以便對父文件夾執行的操作也必須顯示在子文件夾中。方法必須遞歸調用,但我不知道如何繼續進一步。如何查找文件夾然後子文件夾,然後在戶外打印文件名

請在這裏指導我。謝謝..

+0

這不是遞歸方法調用的示例。你有'executeImpl'方法似乎開始的東西,但循環功能需要一些不同的方法遞歸。 – JRSofty

+0

所以我們不能在executeImpl方法中執行遞歸操作?通過爲文件夾創建一個「for循環」? – APM

+0

這取決於你的文件夾結構有多深。如果你知道確切的深度,那麼也許,但這將是非常難看和難以閱讀。 – JRSofty

回答

1

它看起來好像你在理解如何爲你的文件夾創建遞歸操作時遇到問題。我會試着給你舉例說明我將如何處理這個問題。鑑於我的例子可能不是最好的。

public static final String NAME = "Demo-Folder"; 
public static final String PARAM_ASPECT_NAME = "folder-name"; 
private NodeService nodeService; 
/** 
* Set the node service 
* 
* @param nodeService the node service 
*/ 
public void setNodeService(NodeService nodeService) 
{ 
    this.nodeService = nodeService; 
} 

public void executeImpl(Action action, NodeRef actionUponNodeRef) 
{ 
    ChildAssociationRef childAssociationRef = nodeService.getPrimaryParent(actionUponNodeRef); 

    System.out.println("****The folder is***** "+ childAssociationRef); 
    iterateThroughChildren(childAssociationRef); 
} 

public void iterateThroughChildren(ChildAssociationRef childAssocRef) 
{ 
    System.out.println("****The folder is***** "+ childAssocRef); 
    NodeRef childNodeRef = childAssocRef.getChildRef(); 
    List<ChildAssociationRef> children = nodeService.getChildAssocs(childNodeRef); 

    for (ChildAssociationRef childAssoc : children) 
    { 
     childAssoc.getChildRef(); 
     // Use childNodeRef here. 
     System.out.println("******Documents inside are******"+ childAssoc); 

     // This call recurses the method with the new child. 
     iterateThroughChildren(childAssoc); 
     // If there are no children then the list will be empty and so this will be skipped. 


    } 
} 

我用你的代碼上面,並擴展它包括一個名爲iterateThroughChildren新方法,採取ChildAssociationRef,並通過他們得到孩子和迭代。在迭代過程中,孩子們被傳回到方法中以允許他們的孩子被迭代,等等,直到沒有孩子(意味着你在一個分支的末尾)。這使您可以遍歷整個樹結構。

+0

讓我試試這個 – APM

+0

代碼給出了錯誤 – APM

+0

我看到它了,代碼複製/粘貼的另一個bug。堅持我會解決它。基本上'iterateThroughChildren'的前兩行使用了方法內部不存在的變量。對於那個很抱歉。請參閱編輯我的更正。 – JRSofty

相關問題