java 遍历文件夹获取路径和名称

来源:互联网 发布:网络开设赌场被判案例 编辑:程序博客网 时间:2024/05/21 09:38
public void getFiles(String filePath){    File root = new File(filePath);    File[] files = root.listFiles();    for(File file:files){            if(file.isDirectory()){        getFiles(file.getAbsolutePath());        //filelist.add(file.getAbsolutePath());        System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath());    }else{        System.out.println("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath());    }    }}

获取路径后面的文件名称:

String newFilePath = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\")+1);
0 0
原创粉丝点击