API for accessing hierarchical file systems. Clients use Path
s to
obtain Resource
s representing files or directories.
Path
contains many useful operations for manipulating paths:
value path = parsePath("/Users/Trompon/Documents"); value child = path.childPath("hello.txt"); value sibling = child.siblingPath("goodbye.txt"); value parent = path.parent;
The attribute resource
of Path
is used to obtain a Resource
. It
is usually necessary to narrow a Resource
to a File
, Directory
,
Link
, or Nil
before performing operations on it.
To create a file named hello.txt
in the home directory, we could do
the following:
value filePath = home.childPath("hello.txt"); if (is Nil loc = filePath.resource) { value file = loc.createFile(); value writer = file.writer(); try { writer.writeLine("Hello, World!"); } finally { writer.destroy(); } } else { print("file already exists"); }
To print the contents of the file we just created, we could do this:
value filePath = home.childPath("hello.txt"); if (is File file = filePath.resource) { value reader = file.reader(); try { print(reader.readLine()); } finally { reader.destroy(); } } else { print("file does not exist"); }
Now, to rename the file:
value filePath = home.childPath("hello.txt"); if (is File file = filePath.resource) { value newPath = filePath.siblingPath("goodbye.txt"); if (is Nil loc = newPath.resource) { file.move(loc); } else { print("target file already exists"); } } else { print("source file does not exist"); }
To list the contents of a directory, we have two possibilities. We can list just the direct contents:
if (is Directory dir = home.resource) { for (path in dir.childPaths()) { print(path); } } else { print("directory does not exist"); }
Alternatively, we can create a visitor that walks the whole directory tree rooted at a given path:
object visitor extends Visitor() { shared actual void file(File file) { print(file.path); } } home.visit(visitor);
File systems other than the default file system are supported. For
example, a file system for a zip file may be created using the
convenience function createZipFileSystem()
.
value zipPath = home.childPath("myzip.zip"); if (is Nil|File loc = zipPath.resource) { value zipSystem = createZipFileSystem(loc); value entryPath = zipSystem.parsePath("/hello.txt"); if (is Nil entry = entryPath.resource) { value filePath = home.childPath("hello.txt"); if (is File file = filePath.resource) { file.copy(entry); } else { print("source file does not exist"); } } else { print("entry already exists"); } zipSystem.close(); }
Packages | |
ceylon.file | Public API for the file system module. |
ceylon.file.internal | Internal implementation of the file system module, using the Java SDK. |
Public API for the file system module.
Attributes | |
current | Source Code shared Path current The |
defaultSystem | Source Code shared defaultSystem defaultSystem A |
home | Source Code shared Path home The |
rootDirectories | Source Code shared Directory[] rootDirectories The See also: defaultSystem |
rootPaths | Source Code shared Path[] rootPaths The See also: defaultSystem |
stores | Source Code shared Store[] stores The See also: defaultSystem |
Methods | |
createSystem | Source Code shared System createSystem(String uriString, <String->String>[] properties) Create a Parameters:
|
createZipFileSystem | Source Code Create a Parameters:
|
parsePath | Source Code shared Path parsePath(String pathString) Obtain a See also: defaultSystem |
parseURI | Source Code shared Path parseURI(String uriString) Obtain a See also: System |
sameFile | Source Code |
Interfaces | |
Directory | Source Code shared Directory Represents a directory in a hierarchical file system. |
ExistingResource | Source Code shared ExistingResource A resource that actually exists—that is one that is
not |
File | Source Code shared File Represents a file in a hierarchical file system. |
Link | Source Code shared Link Represents a symbolic link. |
Nil | Source Code shared Nil Represents the absence of any existing file or directory at a certain path in a hierarchical file system. |
Path | Source Code shared Path Represents a path in a hierarchical file system. A path is a sequence of path elements. A path may be an absolute path that begins at the root of the file system, or a relative path. |
Reader | Source Code shared Reader Reads lines of text from a |
Resource | Source Code shared Resource Represents a file, link, or directory located at a certain path, or the absence of a file or directory at that path. |
Store | Source Code shared Store Represents a file system store. |
System | Source Code shared System Represents a special-purpose file system. |
Writer | Source Code shared Writer Writes text to a |
Classes | |
Visitor | Source Code shared Visitor A file visitor. |
defaultSystem | Source Code shared defaultSystem A |
Exceptions | |
NoSuchPrincipalException | Source Code shared NoSuchPrincipalException Thrown if there is no principal with the specified name. |