1

I am working on a method where I need complete folder path like /abc/xyz/mln/ for which I am using recurring method call as below sample code:

public String getFolderDetails(String userid, Long folderId) 
            {
        String folderName = "";
        String folderPath = "";

        try {
                folderName = documentFilesName.getString("name");
                parentFolderId= documentFilesName.getLong("parentFolderId");
                if(documentFilesName.has("parentFolderId"));
                {

                    if(parentFolderId.exist)
                    {
                        folderPath = folderPath+"/"+folderName;
                        getFolderDetails(userid, parentFolderId);
                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return folderPath;
    }

but every time folderName and folderPath gets assigned to "" due to recurring it doesn't keep/append the folderName and everytime new value gets assigned to folderPath.

Whats the better approach I can use here? same is happening with StringBuffer/Builder it create new object always.

3 Answers 3

2
public String getFolderDetails(String userid, Long folderId) {
  // This will be the start of recursion so initializing foldername and folderpath with blank 
  getFolderDetails(userid, folderId, new StringBuilder(""), new StringBuilder(""));
}

// recursive method takes two extra parameters for path and name
private String getFolderDetails(String userid, Long folderId, StringBuilder foldername, StringBuilder folderpath) {
    /// Then each time you dont create a new builder just append to it
} 

You need to pass the state i.e. StringBuilders to the recursive method so that it can keep appending to it.

folderpath.append(File.separator).append(folderName);

You should not use '/' as hard coded while using path, as it is file system dependent, rather you should use File.separator for that as shown above.

Sign up to request clarification or add additional context in comments.

Comments

1

You could provide the folderName and folderPath variables as parameters to your method, thus this: public String getFolderDetails(String userid, Long folderId) becomes this: public String getFolderDetails(String userid, Long folderId, String folderName, String folderPath).

For the first method call, you would provide the empty string variables and then, when you do the recursive call just do getFolderDetails(userid, parentFolderId, folderName, folderPath);

Alternatively, you could have an external StringBuilder which gets populates within the method itself, thus this line: folderPath = folderPath+"/"+folderName; would look something like stringBuilder.append("/").append(folderName);.

2 Comments

thanks :), it sound good and simple solution but again after "return folderPath;" cursor comes to "getFolderDetails(userid, parentFolderId);" how we can stop that?
@nilFi: Please update your question to show what the problem is.
1

You can always avoid recursion by using a stack (LinkedList)

LinkedList<File> stack = new LinkedList<>();
stack.add(root);

while (!stack.isEmpty()) {
    File current = stack.removeFirst();

    ...
    if (someCondition) {
        stack.addLast(someChildFile);
    }
}

1 Comment

it seems interesting but I am bit confused here and unable to understand how it can work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.