-1

I have been given an answer in question: Create folders from folder id and parent id in java

It was very useful but I do not understand the last part of the answer:

"Finally, just grab the folder with id 0 and start building your Files on the disk, using folder.getChildren() as a convenient way to move down the tree. Check out the javadoc on the File object, you will particularly want to use the mkdirs() method."

I don't even know where to being to implement it. Does anyone understand it?

Thank You

1 Answer 1

1

Once your tree of Folder instances has been built, the root of the tree is the Folder with ID 0.

Start with this folder, and create a directory in the file system for each of its children, recursively:

/**
 * Creates a directory in parentDirectory for every child of the given folder,
 * recursively.
 */
public void createDirectoriesForChildren(Folder folder, File parentDirectory) {
    for (Folder childFolder : folder.getChildren()) {
        File directory = new File(parentDirectory, childFolder.getName());
        directory.mkdirs();
        createDirectoriesForChildren(childFolder, directory);
    }
}
5
  • Where about should I place this code? under: for(Folder folder : data.values()) { int parentId = folder.getParentFolderId(); Folder parentFolder = data.get(parentId); if(parentFolder != null) parentFolder.addChildFolder(folder); } or in a new class (called MkDirs and call it with: new MkDirs();? Thanks Again
    – WookooUK
    Sep 29, 2011 at 15:31
  • Do what you prefer. It doesn't matter much. Just try to keep methods small, and to give them only one responsibility.
    – JB Nizet
    Sep 29, 2011 at 16:21
  • epic, I don not understand your code but am I able to just drop it in at the end without passing it anything?
    – WookooUK
    Sep 29, 2011 at 16:26
  • Don't just drop it somewhere. Try to understand it. Read the documentation of the classes and methods it uses. There are just 4 lines, and they should be easy to understand. You need to understand what you're doing if you want to be a developer.
    – JB Nizet
    Sep 29, 2011 at 16:29
  • Ok, I am learning as much as I can as quick as I can. I'm a Linux systems admin by day but always trying to learn new skills. Thanks again
    – WookooUK
    Sep 29, 2011 at 16:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.