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
-
- Object
- AbstractResource
- FileSystemResource
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'sResourceUtils.getFile:file:my%20dir/a.txtismy 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, ornullwhen 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;
falsefor 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:, ...), ornullwhen it has none.no setteroverride
Methods
-
contentLength(
) → int -
The size of the file in bytes, for a
Content-Lengthheader for example. Throws when the file does not exist. -
createRelative(
String relativePath) → FileSystemResource -
The file at
relativePathfrom this file's directory, such as a sibling;relativePathmay 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
UrlResourcecannot ask the server synchronously and always saystrue.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) toend(excluded), the whole file by default, without holding it in memory: for a download or an HTTPRangerequest. -
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
otheris a file resource with the same normalized path.override