zoukankan      html  css  js  c++  java
  • 列出文件夹中分级目录java

    package test;
    
    import java.io.File;
    
    public class exportFileName {
        public static void main(String[] args) {
            export("D:/test/");
        }
        
        private static void export(String filePath){
            if(filePath==null){
                System.out.println("传入路径为空。");
                return;
            }
            if(filePath.endsWith("\")){
                filePath = filePath.replaceAll("\", "/");
            }
            
            File path = new File(filePath);
            if(!path.exists()){
                System.out.println(filePath+"路径不存在。");
                return;
            }
            
            File[] files = path.listFiles();
            if(files!=null&&files.length>0){
                for(File file : files){
                    getNames(file,file.getPath());
                    System.out.println("");
                }
            }
        }
        
        private static String getNames(File file, String parentName){
            if(file==null){
                return null;
            }
            
            if(file.isDirectory()){
                String pathName = file.getName();
                if(!parentName.contains(pathName)){
                    String p = file.getParentFile().getName();
                    String pTab = parentName.replaceAll(p, "");
                    pTab = pTab.replaceAll("\|-", "");
                    
                    String tab = "	";
                    pathName = pTab + tab +"|-"+pathName;
                }
                
                System.out.println(pathName);
                
                File[] files = file.listFiles();
                if(files==null||files.length==0){
                    return null;
                }
                for(File f: files){
                    getNames(f,pathName);
                }
            }else if(file.isFile()){
                String fileName = file.getName();
                if(!parentName.contains(fileName)){
                    String p = file.getParentFile().getName();
                    String pTab = parentName.replaceAll(p, "");
                    pTab = pTab.replaceAll("\|-", "");
                    
                    String tab = "	";
                    fileName = pTab + tab + "|-" + fileName;
                }
    
                System.out.println(fileName);
                return fileName;
            }
            return null;
        }
    }
  • 相关阅读:
    链表-(1)
    爬虫(10-3)验证码图片识别
    爬虫10-2(多线程爬虫)
    分布式爬虫系统的架构(19)
    pipenv管理Python虚拟环境
    peewee-async集成到tornado
    Python3笔记051
    Python3笔记050
    Python3笔记049
    Python3笔记048
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/12582010.html
Copyright © 2011-2022 走看看