|
| 1 | +## 588. Design In-Memory File System |
| 2 | + |
| 3 | +### Question |
| 4 | +Design an in-memory file system to simulate the following functions: |
| 5 | + |
| 6 | +ls: Given a path in string format. If it is a file path, return a list that only contains this file's name. If it is a directory path, return the list of file and directory names in this directory. Your output (file and directory names together) should in lexicographic order. |
| 7 | + |
| 8 | +mkdir: Given a directory path that does not exist, you should make a new directory according to the path. If the middle directories in the path don't exist either, you should create them as well. This function has void return type. |
| 9 | + |
| 10 | +addContentToFile: Given a file path and file content in string format. If the file doesn't exist, you need to create that file containing given content. If the file already exists, you need to append given content to original content. This function has void return type. |
| 11 | + |
| 12 | +readContentFromFile: Given a file path, return its content in string format. |
| 13 | + |
| 14 | +``` |
| 15 | +Example: |
| 16 | +
|
| 17 | +Input: |
| 18 | +["FileSystem","ls","mkdir","addContentToFile","ls","readContentFromFile"] |
| 19 | +[[],["/"],["/a/b/c"],["/a/b/c/d","hello"],["/"],["/a/b/c/d"]] |
| 20 | +
|
| 21 | +Output: |
| 22 | +[null,[],null,null,["a"],"hello"] |
| 23 | +
|
| 24 | +Explanation: |
| 25 | +filesystem |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +Note: |
| 30 | +1. You can assume all file or directory paths are absolute paths which begin with / and do not end with / except that the path is just "/". |
| 31 | +2. You can assume that all operations will be passed valid parameters and users will not attempt to retrieve file content or list a directory or file that does not exist. |
| 32 | +3. You can assume that all directory names and file names only contain lower-case letters, and same names won't exist in the same directory. |
| 33 | + |
| 34 | +### Thinking: |
| 35 | +* Method: Trie Tree like design |
| 36 | + ```Java |
| 37 | + class FileSystem { |
| 38 | + private static final class File{ |
| 39 | + boolean isFile; |
| 40 | + TreeMap<String, File> childs; //key is the name, value is the file object. |
| 41 | + String name; |
| 42 | + String content; |
| 43 | + public File(String name){ |
| 44 | + this.name = name; |
| 45 | + this.childs = new TreeMap<>(); |
| 46 | + } |
| 47 | + } |
| 48 | + private File root; |
| 49 | + public FileSystem() { |
| 50 | + this.root = new File("/"); |
| 51 | + } |
| 52 | + |
| 53 | + public List<String> ls(String path) { |
| 54 | + String[] tokens = path.split("/"); |
| 55 | + File cur = root; |
| 56 | + for(int i = 1; i < tokens.length; i++){ |
| 57 | + cur = cur.childs.get(tokens[i]); |
| 58 | + } |
| 59 | + List<String> result = new ArrayList<>(); |
| 60 | + if(cur.isFile){ |
| 61 | + result.add(cur.name); |
| 62 | + }else{ |
| 63 | + for(String key : cur.childs.keySet()){ |
| 64 | + result.add(key); |
| 65 | + } |
| 66 | + } |
| 67 | + return result; |
| 68 | + } |
| 69 | + private File createFileOrDirectory(String path){ |
| 70 | + File cur = root; |
| 71 | + String[] tokens = path.split("/"); |
| 72 | + for(int i = 1; i < tokens.length; i++){ |
| 73 | + if(!cur.childs.containsKey(tokens[i])){ |
| 74 | + File next = new File(tokens[i]); |
| 75 | + cur.childs.put(tokens[i], next); |
| 76 | + } |
| 77 | + cur = cur.childs.get(tokens[i]); |
| 78 | + } |
| 79 | + return cur; |
| 80 | + } |
| 81 | + public void mkdir(String path) { |
| 82 | + createFileOrDirectory(path); |
| 83 | + } |
| 84 | + |
| 85 | + public void addContentToFile(String filePath, String content) { |
| 86 | + int index = filePath.lastIndexOf("/"); |
| 87 | + File dir = createFileOrDirectory(filePath.substring(0, index)); |
| 88 | + String filename = filePath.substring(index + 1); |
| 89 | + File file = null; |
| 90 | + if(!dir.childs.containsKey(filename)){ |
| 91 | + file = new File(filename); |
| 92 | + dir.childs.put(filename, file); |
| 93 | + file.isFile = true; |
| 94 | + file.content = content; |
| 95 | + }else{ |
| 96 | + file = dir.childs.get(filename); |
| 97 | + file.content += content; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public String readContentFromFile(String filePath) { |
| 102 | + File file = createFileOrDirectory(filePath); |
| 103 | + return file.content; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Your FileSystem object will be instantiated and called as such: |
| 109 | + * FileSystem obj = new FileSystem(); |
| 110 | + * List<String> param_1 = obj.ls(path); |
| 111 | + * obj.mkdir(path); |
| 112 | + * obj.addContentToFile(filePath,content); |
| 113 | + * String param_4 = obj.readContentFromFile(filePath); |
| 114 | + */ |
| 115 | + ``` |
0 commit comments