Module ceylon.file

API for accessing hierarchical filesystems. Clients use Paths to obtain Resources 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 usally 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.close();
    }
}
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.close();
    }
}
else {
    print("file does not exist");
}

Now, to move the file to a different directory:

value newPath = home.childPath("goodbye.txt");
if (is Nil loc = newPath.resource) {
    value filePath = home.childPath("hello.txt");
    if (is File file = filePath.resource()) {
        file.move(loc);
    }
    else {
        print("source file does not exist");
    }
}
else {
    print("target file already exists");
}

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 then 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
PackageDescription
ceylon.file

Public API for the filesystem module.

ceylon.file.internal

Internal implementation of the filesystem module, using the Java SDK.

Package ceylon.file

Public API for the filesystem module.

Attributes
Modifier and TypeName and Description
shared Pathcurrent

The Path representing the user current working directory.

shared defaultSystemdefaultSystem

A System representing the default file system.

shared Pathhome

The Path representing the user home directory.

shared Directory[]rootDirectories

The Directorys representing the root directories of the default file system.

See also: defaultSystem
shared Path[]rootPaths

The Paths representing the root directories of the default file system.

See also: defaultSystem
shared Store[]stores

The Stores representing the stores of the default file system.

See also: defaultSystem
Methods
Modifier and TypeMethod and Description
shared SystemcreateSystem(String uriString, Entry<String,String>[] properties)

Create a System given a URI and a sequence of named values.

shared SystemcreateZipFileSystem(File|Nil file, String encoding)

Create a System for accessing entries in a zip file.

shared PathparsePath(String pathString)

Obtain a Path in the default file system given the string representation of a path.

See also: defaultSystem
shared PathparseURI(String uriString)

Obtain a Path given the string representation of a URI. The scheme determines the file system the path belongs to. The scheme file: refers to the default file system.

See also: System
shared BooleansameFile(File x, File y)
Interfaces
Modifier and TypeDescription
shared Directory

Represents a directory in a hierarchical filesystem.

shared ExistingResource

A resource that actually exists, that is one that is not Nil.

shared File

Represents a file in a hierarchical filesystem.

shared Link

Represents a symbolic link.

shared Nil

Represents the absence of any existing file or directory at a certain path in a hierarchical filesystem.

shared Path

Represents a path in a hierarchical filesystem. A path is a sequence of path elements. A path may be an absolute path that begins at the root of the filesystem, or a relative path.

shared Reader

Reads lines of text from a File.

shared Resource

Represents a file, link, or directory located at a certain path, or the absence of a file or directory at the path.

shared Store

Represents a file system store.

shared System

Represents a special-purpose file system.

shared Writer

Writes text to a File.

Classes
Modifier and TypeDescription
shared Visitor

A file visitor.

shared defaultSystem

A System representing the default file system.

Exceptions
Modifier and TypeDescription
shared NoSuchPrincipalException

Thrown if there is no principal with the specified name.