Generate a "wire tree" in Java efficiently (without using recursion)

I have a question that might be linguistic agnostic, but for this particular implementation I use Java. It is possible and relatively trivial to list folders in a directory - using this function:

private DefaultMutableTreeNode GenerateFSTree(File f) { int i = 0; File[] Children = f.listFiles(); DefaultMutableTreeNode x = new DefaultMutableTreeNode(f.getName()); if ( Children != null ) { for ( i = 0; i < Children.length; i++ ) { File f_cur = Children[i]; if ( f_cur.isDirectory() && ( this.DisplayHidden || !f_cur.isHidden() ) ) { x.add(GenerateFSTree(f_cur)); } } } return x; } 

As you can see, this makes heavy use of recursion to evaluate the file system, and as a result you get the DefaultMutableTreeNode element tree.

Now my question is: is there a faster way to do this? It must be because it's slow. Try doing this on / and it will take forever. However, if I use Nautilus or the built-in Java file selection dialog, the tree will be displayed instantly.

So my question is: how can I speed this up?

thanks

+4
source share
2 answers

take a look at this example http://www.java2s.com/Code/Java/Swing-JFC/FileTree.htm
this treemodel will provide the file system structure to your jtree. the file system is only available with the extension node ... and initially for the root node .;)

 class FileTreeModel implements TreeModel { protected File root; public FileTreeModel(File root) { this.root = root; } public Object getRoot() { return root; } public boolean isLeaf(Object node) { return ((File)node).isFile(); } public int getChildCount(Object parent) { String[] children = ((File)parent).list(); if (children == null) return 0; return children.length; } public Object getChild(Object parent, int index) { String[] children = ((File)parent).list(); if ((children == null) || (index >= children.length)) return null; return new File((File) parent, children[index]); } public int getIndexOfChild(Object parent, Object child) { String[] children = ((File)parent).list(); if (children == null) return -1; String childname = ((File)child).getName(); for(int i = 0; i < children.length; i++) { if (childname.equals(children[i])) return i; } return -1; } public void valueForPathChanged(TreePath path, Object newvalue) {} public void addTreeModelListener(TreeModelListener l) {} public void removeTreeModelListener(TreeModelListener l) {} } 

using

 Jtree tree = new JTree(new FileTreeModel(new File("/"))); 

this model will not allow you to fill your memory with DefaultTreeNodes and will minimize access to the underlying file system.

+2
source

The usual way to do this is to read only one directory first. Any directories in this top-level directory will not be read right away. Instead, a node layout will be added to the node directory tree. When the user expands the node directory, the program then reads the corresponding directory and then replaces the node mannequin with nodes that reflect the contents of the directory.

+4
source

All Articles