美文网首页
Java实现根据文件目录结构,构建父子级、祖孙级关系

Java实现根据文件目录结构,构建父子级、祖孙级关系

作者: Js_Gavin | 来源:发表于2021-03-23 20:49 被阅读0次
    public class FileLevelCode {
    
        private static final String PATH = "D:\\Barea\\Files\\text\\level\\中国";
    
        @Test
        public void main() throws IOException {
            Path path = Paths.get(PATH);
            FileLevelSimpleFileVisitor levelFileVisitor = new FileLevelSimpleFileVisitor(path);
            Files.walkFileTree(path, levelFileVisitor);
            List<Level> levelList = levelFileVisitor.getLevels().stream().sorted(Comparator.comparing(e -> e.getId())).collect(Collectors.toList());
            for (Level level : levelList) {
                System.out.println(level);
            }
        }
    }
    
    public class FileLevelSimpleFileVisitor extends SimpleFileVisitor<Path> {
        private Map<String, Level> levelMap = new HashMap<>();
        private Path path;
        private String rootName;
        private static int nextId = 100;
    
        public FileLevelSimpleFileVisitor(Path path) {
            this.path = path;
            this.rootName = path.getFileName().toString();
        }
    
        public synchronized static int getNextId() {
            return nextId++;
        }
    
        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Path path = Paths.get(rootName, Paths.get(this.path.toString()).relativize(dir).toString());
            if (path.getParent() == null) {
                Level level = new Level(getNextId(), 0, path.getFileName().toString(), "0");
                level.setLevel(1);
                levelMap.put(path.toString(), level);
            } else {
                Level parentLevel = levelMap.get(path.getParent().toString());
                if (parentLevel != null) {
                    parentLevel.setLeaf(true);
                    parentLevel.setChildCount(parentLevel.getChildCount() + 1);
                    Level level = new Level(getNextId(), parentLevel.getId(), path.getFileName().toString(), getLevelCode(parentLevel));
                    level.setLevel(parentLevel.getLevel() + 1);
                    levelMap.put(path.toString(), level);
                }
            }
            return super.preVisitDirectory(dir, attrs);
        }
    
        private String getLevelCode(Level parentLevel) {
            return parentLevel.getCode() + "," + parentLevel.getId();
        }
    
        public List<Level> getLevels() {
            return new LinkedList<>(levelMap.values());
        }
    }
    
    public class Level {
        private int id;
        private int pid;
        private int level;
        private String name;
        private String code;
        private boolean leaf;
        private int childCount;
    
        public Level() {
        }
    
        public Level(int id, int pid, String name, String code) {
            this.id = id;
            this.pid = pid;
            this.name = name;
            this.code = code;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getId() {
            return id;
        }
    
        public void setPid(int pid) {
            this.pid = pid;
        }
    
        public int getPid() {
            return pid;
        }
    
        public int getLevel() {
            return level;
        }
    
        public void setLevel(int level) {
            this.level = level;
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public boolean isLeaf() {
            return leaf;
        }
    
        public void setLeaf(boolean leaf) {
            this.leaf = leaf;
        }
    
        public int getChildCount() {
            return childCount;
        }
    
        public void setChildCount(int childCount) {
            this.childCount = childCount;
        }
    
        @Override
        public String toString() {
            return "Level{" +
                    "id=" + id +
                    ", pid=" + pid +
                    ", level=" + level +
                    ", code='" + code + '\'' +
                    ", name='" + name + '\'' +
                    ", leaf=" + leaf +
                    ", childCount=" + childCount +
                    '}';
        }
    }
    

    运行结果

    Level{id=100, pid=0, level=1, code='0', name='中国', leaf=true, childCount=5}
    Level{id=101, pid=100, level=2, code='0,100', name='上海', leaf=true, childCount=3}
    Level{id=102, pid=101, level=3, code='0,100,101', name='徐汇区', leaf=false, childCount=0}
    Level{id=103, pid=101, level=3, code='0,100,101', name='晋安区', leaf=false, childCount=0}
    Level{id=104, pid=101, level=3, code='0,100,101', name='黄浦区', leaf=false, childCount=0}
    Level{id=105, pid=100, level=2, code='0,100', name='北京', leaf=true, childCount=4}
    Level{id=106, pid=105, level=3, code='0,100,105', name='东城区', leaf=false, childCount=0}
    Level{id=107, pid=105, level=3, code='0,100,105', name='朝阳区', leaf=false, childCount=0}
    Level{id=108, pid=105, level=3, code='0,100,105', name='海掟区', leaf=false, childCount=0}
    Level{id=109, pid=105, level=3, code='0,100,105', name='西城区', leaf=false, childCount=0}
    Level{id=110, pid=100, level=2, code='0,100', name='四川', leaf=true, childCount=2}
    Level{id=111, pid=110, level=3, code='0,100,110', name='广安市', leaf=true, childCount=1}
    Level{id=112, pid=111, level=4, code='0,100,110,111', name='前锋区', leaf=false, childCount=0}
    Level{id=113, pid=110, level=3, code='0,100,110', name='成都市', leaf=true, childCount=3}
    Level{id=114, pid=113, level=4, code='0,100,110,113', name='星都区', leaf=false, childCount=0}
    Level{id=115, pid=113, level=4, code='0,100,110,113', name='锦江区', leaf=false, childCount=0}
    Level{id=116, pid=113, level=4, code='0,100,110,113', name='龙泉驿区', leaf=false, childCount=0}
    Level{id=117, pid=100, level=2, code='0,100', name='福建', leaf=true, childCount=3}
    Level{id=118, pid=117, level=3, code='0,100,117', name='厦门市', leaf=true, childCount=3}
    Level{id=119, pid=118, level=4, code='0,100,117,118', name='台江区', leaf=false, childCount=0}
    Level{id=120, pid=118, level=4, code='0,100,117,118', name='同安区', leaf=false, childCount=0}
    Level{id=121, pid=118, level=4, code='0,100,117,118', name='思明区', leaf=false, childCount=0}
    Level{id=122, pid=117, level=3, code='0,100,117', name='泉州市', leaf=false, childCount=0}
    Level{id=123, pid=117, level=3, code='0,100,117', name='福州市', leaf=true, childCount=3}
    Level{id=124, pid=123, level=4, code='0,100,117,123', name='仓山区', leaf=false, childCount=0}
    Level{id=125, pid=123, level=4, code='0,100,117,123', name='集美区', leaf=false, childCount=0}
    Level{id=126, pid=123, level=4, code='0,100,117,123', name='鼓楼区', leaf=false, childCount=0}
    Level{id=127, pid=100, level=2, code='0,100', name='重庆', leaf=true, childCount=4}
    Level{id=128, pid=127, level=3, code='0,100,127', name='巴南区', leaf=true, childCount=2}
    Level{id=129, pid=128, level=4, code='0,100,127,128', name='鱼洞', leaf=false, childCount=0}
    Level{id=130, pid=128, level=4, code='0,100,127,128', name='龙洲湾', leaf=true, childCount=1}
    Level{id=131, pid=130, level=5, code='0,100,127,128,130', name='佳兆业', leaf=false, childCount=0}
    Level{id=132, pid=127, level=3, code='0,100,127', name='江北区', leaf=true, childCount=2}
    Level{id=133, pid=132, level=4, code='0,100,127,132', name='时代天街', leaf=false, childCount=0}
    Level{id=134, pid=132, level=4, code='0,100,127,132', name='观音桥', leaf=false, childCount=0}
    Level{id=135, pid=127, level=3, code='0,100,127', name='沙坪坝区', leaf=true, childCount=2}
    Level{id=136, pid=135, level=4, code='0,100,127,135', name='南开小学', leaf=false, childCount=0}
    Level{id=137, pid=135, level=4, code='0,100,127,135', name='重庆大学', leaf=false, childCount=0}
    Level{id=138, pid=127, level=3, code='0,100,127', name='渝中区', leaf=true, childCount=3}
    Level{id=139, pid=138, level=4, code='0,100,127,138', name='大坪', leaf=false, childCount=0}
    Level{id=140, pid=138, level=4, code='0,100,127,138', name='奥体', leaf=false, childCount=0}
    Level{id=141, pid=138, level=4, code='0,100,127,138', name='解放碑', leaf=false, childCount=0}
    

    相关文章

      网友评论

          本文标题:Java实现根据文件目录结构,构建父子级、祖孙级关系

          本文链接:https://www.haomeiwen.com/subject/jikqhltx.html