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; } }