Class FileWriter

Convenience class for writing character files.

Files are stored in a shared file system where multiple processes could access the same file. The client code is responsible for ensuring that no more than one process writes to a file at a given time.

Note: when this class is used with sensitive data, be careful in persisting sensitive information to disk.

PropertyDescription
lineSeparator: StringGet the current line separator (e.g.
ConstructorDescription
FileWriter(File)Constructs the writer for the specified file.
FileWriter(File, Boolean)Constructs the writer for the specified file.
FileWriter(File, String)Constructs the writer for the specified file with the specified encoding.
FileWriter(File, String, Boolean)Constructs the writer for the specified file with the specified encoding.
MethodDescription
close()Closes the writer.
getLineSeparator()Get the current line separator (e.g.
setLineSeparator(String)Set the line separator (e.g.
writeLine(String)Writes the specified line and appends the line separator.

close, flush, write, write

assign, create, create, defineProperties, defineProperty, entries, freeze, fromEntries, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, hasOwnProperty, is, isExtensible, isFrozen, isPrototypeOf, isSealed, keys, preventExtensions, propertyIsEnumerable, seal, setPrototypeOf, toLocaleString, toString, valueOf, values

lineSeparator: String

Get the current line separator (e.g. '\n' or '\r\n'), if no value is set the system default '\n' will be used.


FileWriter(file: File)

Constructs the writer for the specified file. Uses "UTF-8" as encoding.

To release system resources, close the writer by calling close().

Parameters:

  • file - the file object to write to.

FileWriter(file: File, append: Boolean)

Constructs the writer for the specified file. Optional file append mode is supported. Uses "UTF-8" as encoding.

To release system resources, close the writer by calling close().

Parameters:

  • file - the file object to write to.
  • append - flag, whether the file should be written in append mode

FileWriter(file: File, encoding: String)

Constructs the writer for the specified file with the specified encoding.

To release system resources, close the writer by calling close().

Parameters:

  • file - the file object to write to.
  • encoding - the character encoding to use.

FileWriter(file: File, encoding: String, append: Boolean)

Constructs the writer for the specified file with the specified encoding. Optional file append mode is supported.

To release system resources, close the writer by calling close().

Parameters:

  • file - the file object to write to.
  • encoding - the character encoding to use.
  • append - flag indicating whether the file should be written in append mode.

close(): void

Closes the writer.


getLineSeparator(): String

Get the current line separator (e.g. '\n' or '\r\n'), if no value is set the system default '\n' will be used.


setLineSeparator(lineSeparator: String): void

Set the line separator (e.g. '\n' or '\r\n'), if no value is set the system default '\n' will be used.

Parameters:

  • lineSeparator - that will be written at the end of each line

writeLine(str: String): void

Writes the specified line and appends the line separator.

Parameters:

  • str - the line to write to the file.