如何用“/”分隔arraylist <file>

时间:2015-12-12 18:33:56

标签: java arraylist

我正在做一个shell。当用户输入cd "path"时,在控制台上当前目录将在给定路径中更改。当用户输入find "filename"时,我的程序将在该目录中搜索该文件并返回该目录的路径。

我编写了一个打印所有文件的方法,包括该目录下的子目录......但我不明白如何找到用户输入的特定文件并打印文件路径。

我认为我必须按/分割路径,而不是将它们存储到ArrayList中,而不是将它们与用户输入"filename"相等。我不知道如何将路径拆分为ArrayList<File>

我的方法的当前状态:

public static void findFilesInDirectory()
    {
        ArrayList<File> anArray = new ArrayList<File>();
        String getDirectoryName = presentWorkingDirectory;

        findFilesProcess(getDirectoryName, anArray);


        for(File str: anArray)
        {

            System.out.println("Hamza Found File: " + str);

        }
    }
public static void findFilesProcess(String directoryName, ArrayList<File> files) 
{

        File directory = new File(directoryName);
        File[] fList = directory.listFiles();
        for (File file : fList)
        {
            if (file.isFile())
            {
                files.add(file);
            } 
            else if (file.isDirectory())
            {
                findFilesProcess(file.getAbsolutePath(), files);

            }

        }

3 个答案:

答案 0 :(得分:1)

查找目录中的文件:

public static File[] findFilesInDirectory(String presentWorkingDirectory) {
    // your directory
    File f = new File(presentWorkingDirectory);
    File[] matchingFiles = f.listFiles();
    return matchingFiles;
}

答案 1 :(得分:1)

如果您只有字符串目录结构而不是实际目录,则可以使用以下代码段仅从目录结构中获取文件名或仅获取文件名:

class X { print("X") }
class A extends X { print("A") }
trait H { print("H") }
trait S extends H { print("S") }
trait R { print("R") }
trait T extends R with H { print("T") }
class B extends A with T with S { print("B") }

new B  // X A R H T S B     (the prints follow the construction order)

// Linearization is the reverse of the construction order.
// Note: the rightmost "H" wins (traits are not re-constructed)
// lin(B) = B >> lin(S) >> lin(T) >> lin(A)
//        = B >> (S >> H) >> (T >> H >> R) >> (A >> X)
//        = B >> S >> T >> H >> R >> A >> X

输出:

public static void main(String[] args)
    {
        List<String> dirPaths = new ArrayList<String>();
        dirPaths.add("/dir1/dir2/myfile1.txt");
        dirPaths.add("/dir1/myfile1.txt");
        dirPaths.add("/dir1/dir2/myfile2.txt");
        dirPaths.add("myfile.txt");
        for (String string : dirPaths)
        {
            String[] split = string.split("/");
            System.out.println(split[split.length - 1]);
        }
    }

希望它对你有所帮助。祝你今天愉快。 : - )

答案 2 :(得分:0)

好的@muhammad让我们这样做。

public static void main(String[] args)
    {
        // User input file name
        final String searchFileName = "";
        File f = null;
        File[] paths;
        try
        {
            // Your current working dir
            f = new File("c:/test");

            // create new filename filter
            FilenameFilter fileNameFilter = new FilenameFilter()
            {

                public boolean accept(File dir, String name)
                {
                    if (searchFileName.equalsIgnoreCase(name))
                    {
                        return true;
                    }
                    return false;
                }
            };
            // returns pathnames for files and directory filtered by user search file name, now you just have to show path as per your requirement.
            paths = f.listFiles(fileNameFilter);

            // for each pathname in pathname array
            for (File path : paths)
            {
                // prints file and directory paths
                System.out.println("paths:" + path + " :: file name" + path.getName());
            }
        }
        catch (Exception e)
        {
            // if any error occurs
            e.printStackTrace();
        }
    }

希望至少此代码段可以帮助您。祝你今天愉快。 : - )