排序树格式文件

时间:2017-09-13 05:44:07

标签: java sorting

我必须以特定格式对文本文件进行排序。 我的档案:

AAAA
    BBBB
        CCCC
        CCCC
        CCCC
AAAA
    BBBB
        CCCC
    BBBB
        CCCC

我必须单独排序AAAA (first)BBBB (second) AAAACCCC (third) BBBB,但我不知道如何制作。我知道在AAAABBBBCCCC之间有4个空格。

我可以将它解析为JSON,我认为这样可行但是可能有一些简单的方法来对该文件进行排序。你能给我一些想法吗?

我读过关于获取整个AAAA段但内部是什么?

结果不一定是空格(例如),但它在视觉上更好。

请告诉我你的意见。

示例:

Def
    Abc
        Xyz
        Ghi
        Def
Abc
    Def
        Xyz
    Abc
        Abc

我的节目输出:

Abc
    Abc
        Abc
    Def
        Xyz
Def
    Abc
        Def
        Ghi
        Xyz

编辑:名称AAAA,BBBB,CCCC将永远不会相同。

1 个答案:

答案 0 :(得分:0)

好的,所以你可以使用这个程序来解决你的问题,我在这里使用了Directory数据结构::

class Directory {
    public String name;
    public List<Directory > children;
}

因此,在此程序的帮助下,您将获得所需排序格式的目录列表。这是我打印列表时的输出,列表是分类格式。

[目录[name = Abc,children = [Directory [name = Abc,children = [Directory [name = Abc,children = null]]],目录[name = Def,children = [Directory [name = Xyz, children = null]]]]],目录[name = Def,children = [Directory [name = Abc,children = [Directory [name = Def,children = null],Directory [name = Ghi,children = null],Directory [name = Xyz,children = null]]]]]]

请参阅该程序,它是自我解释的,如果您有任何疑问,您可以发表评论。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class test {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("Input.txt"));
        List<Directory> dirList = new ArrayList<>();
        try {
            String line = br.readLine();
            while (line != null) {
                int spaces = line.length() - line.replaceAll(" ", "").length();
                while (spaces == 0) {
                    Directory directory = new Directory();
                    directory.name = line.replaceAll(" ", "");
                    directory.children = new ArrayList<>();
                    line = br.readLine();
                    spaces = line.length() - line.replaceAll(" ", "").length();
                    while (spaces == 4) {
                        Directory directory2 = new Directory();
                        directory2.name = line.replaceAll(" ", "");
                        directory2.children = new ArrayList<>();
                        line = br.readLine();
                        if (line == null)
                            break;
                        spaces = line.length() - line.replaceAll(" ", "").length();
                        while (spaces == 8) {
                            Directory directory3 = new Directory();
                            directory3.name = line.replaceAll(" ", "");
                            directory2.children.add(directory3);
                            line = br.readLine();
                            if (line == null)
                                break;
                            spaces = line.length() - line.replaceAll(" ", "").length();
                        }
                        Collections.sort(directory2.children);
                        directory.children.add(directory2);
                    }
                    Collections.sort(directory.children);
                    System.out.println(directory);
                    dirList.add(directory);
                }
                line = br.readLine();
            }
            Collections.sort(dirList);
            System.out.println(dirList);
        } finally {
            br.close();
        }
    }
}

class Directory implements Comparable<Directory> {
    public String name;
    public List<Directory> children;

    @Override
    public String toString() {
        return "Directory [name=" + name + ", children=" + children + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Directory other = (Directory) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    public void sort() {
        Collections.sort(children);
    }

    @Override
    public int compareTo(Directory o) {
        return name.compareTo(o.name);
    }

}