列出Java中目录和子目录中的文件,仅包括部分文件路径 [英] List files from directories and sub directories in java including only partial file paths

查看:65
本文介绍了列出Java中目录和子目录中的文件,仅包括部分文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从给定目录获取文件及其父目录在java中的路径,但不包括它.

I need to get the paths of files and their parent directories in java from a given directory but not including it.

例如,如果给我的方法指定路径:/home/user/test 作为路径,它将返回该目录及其下所有文件的路径.

So for example, If my method was given the path: /home/user/test as a path it would return the paths of all files in that directory and under it.

因此,如果/home/user/test 具有子文件夹:/subdir1 /subdir2 ,每个子文件夹都包含 file1.txt file2.txt ,则该方法的结果将是2个包含/subdir1/file1.txt /subdir2/file2.txt

So if /home/user/test had the sub folders: /subdir1 and /subdir2 each containing file1.txt and file2.txt then the result of the method would be 2 strings containing /subdir1/file1.txt and /subdir2/file2.txt

如果 subdir1 内部有一个名为 subsubdir 的目录,并且在该 file3.txt 中,则为该文件创建的字符串将是/subdir1/subsubdir/file3.txt ,以及是否还有其他子目录可以继续.

And if subdir1 had a directory inside it called subsubdir and inside that file3.txt, then the string created for that file would be /subdir1/subsubdir/file3.txt, and if there are further sub directories that would continue.

这个想法是我只想要文件上方的目录路径,而不是绝对路径,所以只希望初始给定路径之后的目录.

The idea is I just want the directory paths above the file but not the absolute path so only the directories AFTER the initial given path.

我知道它有点混乱,但是我相信有人可以理解它.现在,我所拥有的只是一个递归函数,它可以打印出文件名及其绝对路径.

I know its a little confusing but I'm sure someone can make sense of it. Right now all I have is a recursive function that prints out file names and their absolute paths.

对此有任何帮助吗?

推荐答案

如果您尝试过一些事情并询问有关该问题的话,那会很好.

What would have been nice if you had tried something and asked questions about that...

但是...

public class TestFileSearch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new TestFileSearch();
    }

    public TestFileSearch() {
        File parentPath = new File("C:/Users/shane/Documents");
        List<String> files = list(parentPath);

        for (String file : files) {
            System.out.println(file);
        }
    }

    protected List<String> list(File parent) {
        return listFiles(parent, parent);
    }

    protected List<String> listFiles(File parent, File folder) {
        List<String> lstFiles = new ArrayList<String>(25);
        if (folder.isDirectory()) {

            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        lstFiles.addAll(listFiles(parent, file));
                    } else {
                        String path = file.getPath();
                        String offset = parent.getPath();

                        path = path.substring(offset.length());
                        lstFiles.add(path);
                    }
                }
            }
        }

        return lstFiles;
    }
}

您可以简单地进行普通的文件夹递归,返回文件列表和前缀的THEN条,但这取决于您

You could simply do a normal folder recursion, returning a list of files and THEN strip of the prefix, but that's up to you

这篇关于列出Java中目录和子目录中的文件,仅包括部分文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆