FileSystemResource class

A Resource for a file (or directory) on the local file system, given by its path.

file: locations resolve to it, which is how ./application.yml next to a server binary or file:./data/import.csv is read. An application gets it from a resource loader, or creates one directly for a path it already knows, for example to serve a file with openRead. It can be read synchronously where dart:io exists; on the web it never exists. A directory exists but is not readable (isReadable is false), as in Spring.

// A config file next to the program, with its profile variant.
final resource = FileSystemResource('config/application.properties');
if (resource.exists()) {
  final text = resource.readAsStringSync();
  final dev = resource.createRelative('application-dev.properties');
}

Spring (Java):

var resource = new FileSystemResource("config/application.properties");
if (resource.exists()) {
  String text = resource.getContentAsString(StandardCharsets.UTF_8);
  Resource dev = resource.createRelative("application-dev.properties");
}
Inheritance

Constructors

FileSystemResource(String path)
Creates the resource for path, relative to the working directory or absolute. Nothing is read or checked yet.
FileSystemResource.fromFileUrl(String url)
Creates the resource for a file: URL, percent-decoded like Spring's ResourceUtils.getFile: file:my%20dir/a.txt is my dir/a.txt.
factory

Properties

description → String
A text that tells people which resource this is, for error messages, such as file [/app/config/application.properties]. It is also the default name of a property source read from it, and two resources of the same type with the same description are equal.
no setteroverride
filename → String
The last part of the path, such as application.yml, or null when the resource has no file name (bytes in memory).
no setteroverride
hashCode → int
The hash code for this object.
no setteroverride
isDirectory → bool
Whether path is an existing directory.
no setter
isReadable → bool
Whether this is an existing file, which can be read; false for a directory.
no setteroverride
path → String
The normalized path, relative or absolute as it was given.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
supportsSyncRead → bool
Whether readAsBytesSync and readAsStringSync work. True for files and the embedded classpath; false for URLs and Flutter assets.
no setteroverride
uri → Uri
The URI of the resource (file:, https:, ...), or null when it has none.
no setteroverride

Methods

contentLength() → int
The size of the file in bytes, for a Content-Length header for example. Throws when the file does not exist.
createRelative(String relativePath) → FileSystemResource
The file at relativePath from this file's directory, such as a sibling; relativePath may contain ...
override
exists() → bool
Whether the content is there: the file exists, the classpath entry is embedded. Check it before reading a resource that may be missing. A UrlResource cannot ask the server synchronously and always says true.
override
lastModified() → DateTime
When the file was last changed, for caching headers for example. Throws when the file does not exist.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
openRead([int? start, int? end]) → Stream<List<int>>
Streams the content from byte start (included) to end (excluded), the whole file by default, without holding it in memory: for a download or an HTTP Range request.
readAsBytes() → Future<Uint8List>
Reads the whole content as bytes. Works for every resource that exists; fails for one that does not.
override
readAsBytesSync() → Uint8List
Reads the whole content as bytes without waiting, for code that cannot be asynchronous. Throws UnsupportedError unless supportsSyncRead.
override
readAsString({Encoding encoding = utf8}) → Future<String>
Reads the whole content as text, decoded with encoding (UTF-8 unless given).
inherited
readAsStringSync({Encoding encoding = utf8}) → String
Reads the whole content as text without waiting, decoded with encoding. Throws UnsupportedError unless supportsSyncRead.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
Whether other is a file resource with the same normalized path.
override