FileWriter class

Convenience class for writing character files.

The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.

FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using FileOutputStream.

Example Usage

// Writing to a text file
final writer = FileWriter('output.txt');
try {
  await writer.write('Hello, World!');
  await writer.writeLine();
  await writer.writeLine('This is a new line.');
  await writer.flush();
} finally {
  await writer.close();
}

// Appending to existing file
final writer = FileWriter('log.txt', append: true);
try {
  await writer.writeLine('${DateTime.now()}: New log entry');
  await writer.flush();
} finally {
  await writer.close();
}
Inheritance

Constructors

FileWriter(String fileName, {bool append = false, Encoding encoding = utf8})
Creates a new FileWriter, given the name of the file to write to.
FileWriter.fromFile(File file, {bool append = false, Encoding encoding = utf8})
Creates a new FileWriter, given the File to write to.

Properties

bufferedCount int
Returns the number of characters currently in the buffer.
no setter
encoding Encoding
Returns the encoding used by this writer.
no setter
file File
Returns the File object associated with this writer.
no setter
hashCode int
The hash code for this object.
no setterinherited
isAppendMode bool
Returns whether this writer is in append mode.
no setter
isClosed bool
Returns true if this writer has been closed.
no setterinherited
position int
Returns the current byte position in the file.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

append(int c) Future<Writer>
Appends the specified character to this writer.
inherited
appendString(String? csq) Future<Writer>
Appends the specified character sequence to this writer.
inherited
checkClosed() → void
Checks if the writer is closed and throws an exception if it is.
inherited
close() Future<void>
Closes the stream, flushing it first.
override
flush() Future<void>
Flushes the stream.
override
newLine() Future<void>
Writes a line separator.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
write(String str, [int offset = 0, int? length]) Future<void>
Writes a string.
override
writeChar(int c) Future<void>
Writes a single character.
override
writeChars(List<int> cbuf, [int offset = 0, int? length]) Future<void>
Writes an array of characters.
override
writeLine([String? str]) Future<void>
Writes a string followed by a line separator.
inherited
writeObject(Object? obj) Future<void>
Writes the string representation of an object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited