Design an in-memory file system that stores directories, files, file content, and integer values associated with selected directory paths.
The file system begins with an empty root directory represented by "/".
FileSystem
FileSystem()
Initialize an empty file system containing only the root directory.
List<String> ls(String path)
path: The absolute path of an existing file or directory.If path identifies a file, return a list containing only that file's name.
If path identifies a directory, return the names of its immediate files and subdirectories in lexicographic order.
void mkdir(String path)
path: The absolute directory path to create.Create the directory and every missing intermediate directory in the path. Directories created by this method do not have associated integer values.
void addContentToFile(String filePath, String content)
filePath: The absolute path of the file.content: The text to add to the file.Create the file with the supplied content when it does not exist. Otherwise, append the content to the end of the existing file content.
String readContentFromFile(String filePath)
filePath: The absolute path of an existing file.Return the complete content stored in the file.
boolean createPath(String path, int value)
path: The new absolute directory path to create.value: The integer value to associate with the path.Create the path as a directory with the associated value and return true when the path does not already exist and its immediate parent directory exists.
Return false when the path already exists, its parent does not exist, or its parent is a file. Unlike mkdir, this method does not create missing parent directories.
int get(String path)
path: The absolute path whose associated value is requested.Return the integer value associated with the path. Return -1 if the path does not exist or was created without an associated value.
"/", and does not end with "/", except for the root path "/".createPath.createPath is a directory, can contain other entries, and appears in directory listings.mkdir for an existing directory has no effect. It does not remove or change an existing associated value.mkdir does not assign values to the directories it creates.mkdir has a file as one of its path components.addContentToFile already exists.addContentToFile never identifies an existing directory.ls exist, and paths passed to readContentFromFile identify existing files.10,000 method calls will be made.2 ≤ path.length() ≤ 100 for every path other than "/".2 ≤ filePath.length() ≤ 1001 ≤ content.length() ≤ 50200,000 characters.1 ≤ value ≤ 1,000,000,000FileSystem fileSystem = new FileSystem()
fileSystem.ls(path = "/") returns [] because the root directory is initially empty.
fileSystem.mkdir(path = "/projects/api") creates both directories.
fileSystem.createPath(path = "/projects/api/cache", value = 17) returns true because its parent directory exists.
fileSystem.createPath(path = "/projects/web/ui", value = 25) returns false because the immediate parent path "/projects/web" does not exist.
fileSystem.get(path = "/projects/api/cache") returns 17.
fileSystem.get(path = "/projects/api") returns -1 because the directory was created without an associated value.
fileSystem.addContentToFile(filePath = "/projects/api/readme", content = "hello") creates the file.
fileSystem.addContentToFile(filePath = "/projects/api/readme", content = " world") appends text to the file.
fileSystem.ls(path = "/projects/api") returns ["cache", "readme"].
fileSystem.readContentFromFile(filePath = "/projects/api/readme") returns "hello world".
FileSystem fileSystem = new FileSystem()
fileSystem.createPath(path = "/data", value = 7) returns true.
fileSystem.createPath(path = "/data/logs", value = 11) returns true.
fileSystem.createPath(path = "/data", value = 20) returns false because the path already exists.
fileSystem.mkdir(path = "/data/archive/old") creates the missing directory structure.
fileSystem.addContentToFile(filePath = "/data/archive/note", content = "saved") creates a new file.
fileSystem.ls(path = "/data") returns ["archive", "logs"].
fileSystem.ls(path = "/data/archive/note") returns ["note"].
fileSystem.get(path = "/data/logs") returns 11.
fileSystem.get(path = "/data/archive") returns -1 because that directory has no associated value.
fileSystem.get(path = "/missing") returns -1 because the path does not exist.