将目录树(.txt文件)转换为JTree的算法

时间:2015-08-07 08:37:13

标签: java algorithm swing jtree

我正在制作一个程序,它将生成一个包含目录树的文本文件。我已经完成了文件行走部分,所以我准备好了我的文本文件。然后我想使用这个文本文件,读取它并将数据转换为JTree。我一直坚持算法2天!!有没有人有一些建议?请帮助。

*请注意我使用"\t"作为间距。

我的path.txt

的某些部分

path.txt

Arcana Advanced
        ABC
        ABC
        AcnMiniGame
        client
        Data
        error
        patch
        patch_03451
        patch_03452
        patch_03453
        patch_03454
        patch_03455
        patch_03456
        patch_03458
        SaveDeck
                ModifiedDeck1 Earth Water
                ModifiedDeck2 Wind Fire
                ModifiedDeck3 Wind Earth
                ModifiedDeck4 Earth Fire
                ModifiedDeck5 Wind Water
                ModifiedDeck6 Fire Water
                Starter1 Earth Water
                Starter2 Fire Wind
                Starter3 Earth Wind
                Starter4 Earth Fire
                Starter5 Water Wind
                Starter6 Water Fire
        Tutorial
        unicows
        unins000
        unins000
ASActiveX
        Au_activeX
                ThaiGameStart
        nProtect
                npkcx
                        npkagt
                        npkcrypt
                        npkcrypt
                        npkcrypt
                        npkcsvc
                        npkcusb
                        npkcx
                        npkcx
                        npkpdb
                        npkuninst

这是我到目前为止所尝试的:

public final class FileTree extends JPanel {


JTree tree;
DefaultMutableTreeNode root;
Path path;
List<String> lines;

public FileTree(String filepath) {
    try {
        root = new DefaultMutableTreeNode("root", true);
        this.path = Paths.get(filepath);
        lines = Files.readAllLines(path);
        getList(root, 0);
        setLayout(new BorderLayout());
        tree = new JTree(root);
        tree.setRootVisible(false);
        add(new JScrollPane((JTree) tree), "Center");
    } catch (IOException ex) {
        Logger.getLogger(FileTree.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public int getTab(int line) {
    String text = lines.get(line);
    return text.lastIndexOf("\t");

}

public boolean noChild(int line) {
    if (getTab(line) < getTab(line + 1)) {
        return false;
    }
    return true;

}

public int getLastLine(int line) {
    int myTab = getTab(line);
    int myLine = line+1;
    int i = line+1;
    while (true) {
        if (myTab == getTab(myLine)) {
            return i;
        } else {
            myLine++;
            i++;
        }
    }
}

public int getChildList(int line) {
    int i = 0;
    int ChildTab = getTab(line + 1);
    int myLine = line + 1;
    while (true) {
        if (ChildTab == getTab(myLine)) {
            myLine++;
            i++;
        } else if (ChildTab < getTab(myLine)) {
            myLine++;
        } else if (ChildTab > getTab(myLine)) {
            return i;
        }
    }
}



public void getList(DefaultMutableTreeNode node, int line) {
    try {
        if (noChild(line)) {
            System.out.println("FILE  -  " + lines.get(line));
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(lines.get(line).trim());
            node.add(child);
        } else {
            System.out.println("DIRECTORY  -  " + lines.get(line));
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(lines.get(line).trim());
            node.add(child);
            int ChildList = getChildList(line);
            for (int i = 0; i < ChildList; i++) {
                getList(child, line + i + 1);
            }

        }
    } catch (Exception e) {
    }

}

}

结果:http://www.uppic.org/image-5E7B_55C470B7.jpg

我的代码问题似乎是在完成浏览文件夹后,实际的行不知道它,并继续读取下一行,即探索文件夹中的文件。 (用语言难以解释,抱歉我的英语不好)

第二个问题是它没有读取每个主文件夹,如图所示,程序在完成“Arcana Advanced”文件夹的完成后停止工作。我理解这个问题的原因,所以我尝试使用另一种方法来检查它们有多少主文件夹,并进行for循环。但这是非常混乱和耗时的,任何人都有更简单的方法来做到这一点?

3 个答案:

答案 0 :(得分:0)

我现在无法编写正确的Java代码,但这里有一些伪代码可以工作:

File f = new File () ;
Stack s = new Stack () ;
Node r = new Root () ;
int nTabs = 0 ; // Current number of tabs
while (f) { // while there are remaining lines in f
    String line = f.readLine () ;
    int n = countTabs (line) ;
    if (nTabs == n) {
        r.addChild (new Node(line)) ;
    }
    else if (nTabs == n + 1) {
        Node node = new Node(line) ;
        r.addChild (node) ;
        s.push(r) ;
        r = node ;
        nTabs = nTabs + 1 ;
    }
    else {
        while (n < nTabs) {
            nTabs = nTabs - 1;
            r = s.pop () ;
        }
    }
}

答案 1 :(得分:0)

这是我的解决方案:(请注意,当我复制并粘贴您的文件时,我最终会用8个空格替换每个标签。)

    public void LoadTree(String filename, JTree tree) {

        try (BufferedReader br = Files.newBufferedReader(Paths.get(filename))) {
            int last_tab_length = -1;
            String line;
            DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode("root");
            DefaultMutableTreeNode lastNode = parentNode;
            DefaultTreeModel model = new DefaultTreeModel(parentNode);
            tree.setModel(model);
            while ((line = br.readLine()) != null) {
                int tab_length = 0;
                while (line.startsWith("\t")) {
                    tab_length++;
                    line = line.substring(1);
                }
                String eightSpaces = "        ";
                while (line.startsWith(eightSpaces)) {
                    tab_length++;
                    line = line.substring(eightSpaces.length());
                }
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(line.trim() + ":" + tab_length);

                if (tab_length > last_tab_length) {
                    parentNode = lastNode;
                }
                for (int i = last_tab_length; i > tab_length; i--) {
                    parentNode = (DefaultMutableTreeNode) parentNode.getParent();
                }
                parentNode.add(node);
                last_tab_length = tab_length;
                lastNode = node;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

答案 2 :(得分:0)

最后,我得到了一个问题的解决方案。

public final class FileTree extends JPanel {

JTree tree;
DefaultMutableTreeNode root;
Path path;
List<String> lines;

public FileTree(String filepath) {
    try {
        root = new DefaultMutableTreeNode("root", true);
        this.path = Paths.get(filepath);
        lines = Files.readAllLines(path);
        getList(root, 0);
        setLayout(new BorderLayout());
        tree = new JTree(root);
        tree.setRootVisible(false);
        add(new JScrollPane((JTree) tree), "Center");
    } catch (IOException ex) {
        Logger.getLogger(FileTree.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public int getTab(int line) {
    String text = lines.get(line);
    return text.lastIndexOf("\t");

}

public boolean noChild(int line) {
    if (getTab(line) < getTab(line + 1)) {
        return false;
    }
    return true;

}


public TreeNode getNewNode(int line, int line2, DefaultMutableTreeNode node) {
    TreeNode treenode = node;
    int time = getTab(line) - getTab(line2);
    for (int i = 0; i < time; i++) {
        treenode = treenode.getParent();
    }
    return treenode;

}

public void getList(DefaultMutableTreeNode node, int line) {
    DefaultMutableTreeNode child = new DefaultMutableTreeNode(lines.get(line).trim());
    node.add(child);
    if (line + 1 > lines.size() - 1) {
        System.out.println("Finished");
        return;
    }
    if (!noChild(line)) { // have Children
        getList(child, line + 1);
    } else {             // no Children
        if (getTab(line) > getTab(line + 1)) {
            getList((DefaultMutableTreeNode) getNewNode(line, line + 1, node), line + 1);
        } else if (getTab(line) == getTab(line + 1)) {
            getList(node, line + 1);
        }
    }




}

}