FileSystem实现,递归列出文件

时间:2015-12-12 22:28:47

标签: java recursion

我正在我的程序中实现ls方法。我需要创建一个遍历FileSystem的递归方法。

现在是我的FileSystem实施:

import java.util.ArrayList;

public class FileSystem {
private Directory root;
private Directory wDir;
private ArrayList<File> files = new ArrayList<File>();

// Constructor
public FileSystem() {

}

// Constructor with parameters
public FileSystem(Directory root) {
    this.root = root;
    wDir = root;
    files.add(root);
}

// Returns the FileSystem's files
public ArrayList<File> getFiles() {
    return files;
}

// Returns the working directory
public Directory getWDir() {
    return wDir;
}

// Sets the working directory
public void setWDir(Directory d) {
    wDir = d;
}

// Returns the root file. This will always be / in our program
public File getRoot() {
    return root;
}

public File getFile(File f, String name) {
    if (f.isDirectory()) {
        for (File c : ((Directory) f).getChildren()) {
            if (c.getName().equals(name))
                return c;
        }
    }
    return null;
}

// Currently only used in cat method, getFile is better
File findFile(File f, String name) {
    if (f.getName().equals(name))
        return f;
    File file = null;
    if (f.isDirectory()) {
        for (File c : ((Directory) f).getChildren()) {
            file = findFile(c, name);
            if (file != null)
                break;
        }
    }
    return file;
}

// Returns true if file is found
boolean isFile(String name) {
    File file = null;
    file = getFile(wDir, name);
    if (file != null) {
        return true;
    }
    return false;
}

// Creates Directory
public void mkdir(String path) {
    files.add(new Directory(path));

    int size = files.size();

    // Sets the parent
    files.get(size - 1).setParent(wDir);
    // Sets the child
    wDir.addChild(files.get(size - 1));
}

// Changes working directory
public void cd(String s) {
    if (s.equals("..")) {
        if (wDir != root) {
            wDir = wDir.getParent();
        }
    } else if (s.equals("/")) {
        wDir = root;
    } else {
        wDir = (Directory) getFile(wDir, s);
    }

}

// Provides absolute filename
public void pwd() {
    if (wDir == root) {
        System.out.println("/");
    } else {
        System.out.println(wDir.getPath());
    }
}

// Lists children of current working directory
public void ls() {
    ArrayList<File> children = wDir.getChildren();
    if (children != null) {
        for (int i = 0; i < children.size(); i++) {
            String childName = children.get(i).getName();
            System.out.print(childName + " ");
        }
    }
}

// Lists children of file(s) inputted by user
public void ls(File f) {
    String name = f.getName();
    if (f instanceof TextFile) {
        System.out.println(f.getPath());
    } else {
        ArrayList<File> children = ((Directory) f).getChildren();
        if (children != null) {
            for (int i = 0; i < children.size(); i++) {
                String childName = children.get(i).getName();
                System.out.print(childName + " ");
            }
        }
    }

}

// Creates a TextFile or edit's TextFile's content if already exists in the
// tree
public void edit(String name, String content) {
    files.add(new TextFile(name, content));

    // Setting TextFile parent
    files.get(files.size() - 1).setParent(wDir);
    // Setting Parent's child
    wDir.addChild(files.get(files.size() - 1));

}

// Prints the content of TextFile
public void cat(String name) {
    File f = findFile(root, name);
    System.out.println(((TextFile) f).getContent());
}

}

作为它需要做的一个例子,假设我有一棵这样的树:

        /
       / \
      a   b
     /     \
    x       c
   /         \
  y            d

如果用户输入:ls -r a,我的主类将使用getFile方法转换该String值,我会将其输入到我的递归函数中。然后它将使用ls()ls(File f),我的主程序将输出如下内容:

a:
x

a/x:
y

a/x/y:

我应该如何创建此方法?

另外我应该注意,我有一个Main课程,一个File课程,以及一个继承TextFile的{​​{1}}和Directory课程。

所需的任何其他信息只是让我知道,我将用它来更新这篇文章。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

public void ls(File f) {
    System.out.println(f); //or whatever is needed to print the filename
    if(f instanceof Directory) {
        List<File> fileList = ((Directory)f).getFiles();

        //with Java 8
        fileList.forEach(subFile -> System.out.println(subFile));
        fileList.forEach(subFile -> ls(subFile));

        //without Java 8
        for(File subFile : fileList) {
            System.out.println(subFile);
        }
        for(File subFile : fileList) {
            ls(subFile);
        }


        System.out.println();
    }
}

基本上第一个循环是打印当前目录中的所有文件,第二个循环是为所有子目录执行此操作。如果文件不是目录,则仅打印其名称。在这里,我假设您的Directory类有一个getFiles()方法,该方法返回目录中所有文件的列表