2015-12-12 93 views
0

我正在執行我的程序中的ls方法。我需要創建一個將遍歷我的FileSystem的遞歸方法。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方法轉換該字符串值,並將其輸入到我的遞歸函數中。然後,它會利用任何ls()ls(File f)的,和我的主程序將輸出是這樣的:

a: 
x 

a/x: 
y 

a/x/y: 

我應該怎樣去創造這種方法嗎?我

也要注意,我有一個Main類,一類FileTextFileDirectory類繼承File

任何其他需要的信息只是讓我知道,我會用它更新這篇文章。

回答

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()方法

+0

太棒了,工作。我調整了一下,命名爲'recLS(File f,String location)'然後在開始的時候我'System.out.println(location +「:」);'後面跟着'ls(f)';在第一個循環中,不是打印'subFile',而是執行'location + =「/」+ c.getName();' – Aragorn300