Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Inherited Fields](https://developer.android.com/reference/java/io/FileWriter#inhfields) \| [Ctors](https://developer.android.com/reference/java/io/FileWriter#pubctors) \| [Inherited Methods](https://developer.android.com/reference/java/io/FileWriter#inhmethods)

# FileWriter

` public class FileWriter `   
` ` ` ` ` extends https://developer.android.com/reference/java/io/OutputStreamWriter ` ` `

|---|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||||
| ↳ | [java.io.Writer](https://developer.android.com/reference/java/io/Writer) |||
|   | ↳ | [java.io.OutputStreamWriter](https://developer.android.com/reference/java/io/OutputStreamWriter) ||
|   |   | ↳ | java.io.FileWriter |

<br />

*** ** * ** ***

Writes text to character files using a default buffer size. Encoding from characters to bytes uses either a specified [charset](https://developer.android.com/reference/java/nio/charset/Charset) or the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()).

Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one `FileWriter` (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

The `FileWriter` is meant for writing streams of characters. For writing streams of raw bytes, consider using a `FileOutputStream`.
**See also:**

- `https://developer.android.com/reference/java/io/OutputStreamWriter`
- `https://developer.android.com/reference/java/io/FileOutputStream`

## Summary

### Inherited fields

From class `https://developer.android.com/reference/java/io/Writer` ` protected https://developer.android.com/reference/java/lang/Object` `https://developer.android.com/reference/java/io/Writer#lock`

The object used to synchronize operations on this stream.

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.io.File)(https://developer.android.com/reference/java/io/File file) ` Constructs a `FileWriter` given the `File` to write, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()) |
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.io.File,%20boolean)(https://developer.android.com/reference/java/io/File file, boolean append) ` Constructs a `FileWriter` given the `File` to write and a boolean indicating whether to append the data written, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()). |
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.io.File,%20java.nio.charset.Charset)(https://developer.android.com/reference/java/io/File file, https://developer.android.com/reference/java/nio/charset/Charset charset) ` Constructs a `FileWriter` given the `File` to write and [charset](https://developer.android.com/reference/java/nio/charset/Charset). |
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.io.File,%20java.nio.charset.Charset,%20boolean)(https://developer.android.com/reference/java/io/File file, https://developer.android.com/reference/java/nio/charset/Charset charset, boolean append) ` Constructs a `FileWriter` given the `File` to write, [charset](https://developer.android.com/reference/java/nio/charset/Charset) and a boolean indicating whether to append the data written. |
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.io.FileDescriptor)(https://developer.android.com/reference/java/io/FileDescriptor fd) ` Constructs a `FileWriter` given a file descriptor, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()). |
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.lang.String)(https://developer.android.com/reference/java/lang/String fileName) ` Constructs a `FileWriter` given a file name, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()) |
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.lang.String,%20boolean)(https://developer.android.com/reference/java/lang/String fileName, boolean append) ` Constructs a `FileWriter` given a file name and a boolean indicating whether to append the data written, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()). |
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.lang.String,%20java.nio.charset.Charset)(https://developer.android.com/reference/java/lang/String fileName, https://developer.android.com/reference/java/nio/charset/Charset charset) ` Constructs a `FileWriter` given a file name and [charset](https://developer.android.com/reference/java/nio/charset/Charset). |
| ` https://developer.android.com/reference/java/io/FileWriter#FileWriter(java.lang.String,%20java.nio.charset.Charset,%20boolean)(https://developer.android.com/reference/java/lang/String fileName, https://developer.android.com/reference/java/nio/charset/Charset charset, boolean append) ` Constructs a `FileWriter` given a file name, [charset](https://developer.android.com/reference/java/nio/charset/Charset) and a boolean indicating whether to append the data written. |

### Inherited methods

From class ` https://developer.android.com/reference/java/io/OutputStreamWriter ` ` https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/OutputStreamWriter#append(java.lang.CharSequence,%20int,%20int)(https://developer.android.com/reference/java/lang/CharSequence csq, int start, int end) `

Appends a subsequence of the specified character sequence to this writer.
` https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/OutputStreamWriter#append(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence csq) `

Appends the specified character sequence to this writer.
` void` ` https://developer.android.com/reference/java/io/OutputStreamWriter#close()() `

Closes the stream, flushing it first.
` void` ` https://developer.android.com/reference/java/io/OutputStreamWriter#flush()() `

Flushes the stream.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/io/OutputStreamWriter#getEncoding()() `

Returns the name of the character encoding being used by this stream.
` void` ` https://developer.android.com/reference/java/io/OutputStreamWriter#write(int)(int c) `

Writes a single character.
` void` ` https://developer.android.com/reference/java/io/OutputStreamWriter#write(java.lang.String,%20int,%20int)(https://developer.android.com/reference/java/lang/String str, int off, int len) `

Writes a portion of a string.
` void` ` https://developer.android.com/reference/java/io/OutputStreamWriter#write(char[],%20int,%20int)(char[] cbuf, int off, int len) `

Writes a portion of an array of characters.
From class ` https://developer.android.com/reference/java/io/Writer ` ` https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/Writer#append(char)(char c) `

Appends the specified character to this writer.
` https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/Writer#append(java.lang.CharSequence,%20int,%20int)(https://developer.android.com/reference/java/lang/CharSequence csq, int start, int end) `

Appends a subsequence of the specified character sequence to this writer.
` https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/Writer#append(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence csq) `

Appends the specified character sequence to this writer.
` abstract void` ` https://developer.android.com/reference/java/io/Writer#close()() `

Closes the stream, flushing it first.
` abstract void` ` https://developer.android.com/reference/java/io/Writer#flush()() `

Flushes the stream.
` static https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/Writer#nullWriter()() `

Returns a new `Writer` which discards all characters.
` void` ` https://developer.android.com/reference/java/io/Writer#write(java.lang.String)(https://developer.android.com/reference/java/lang/String str) `

Writes a string.
` void` ` https://developer.android.com/reference/java/io/Writer#write(int)(int c) `

Writes a single character.
` void` ` https://developer.android.com/reference/java/io/Writer#write(java.lang.String,%20int,%20int)(https://developer.android.com/reference/java/lang/String str, int off, int len) `

Writes a portion of a string.
` abstract void` ` https://developer.android.com/reference/java/io/Writer#write(char[],%20int,%20int)(char[] cbuf, int off, int len) `

Writes a portion of an array of characters.
` void` ` https://developer.android.com/reference/java/io/Writer#write(char[])(char[] cbuf) `

Writes an array of characters.
From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/java/lang/Appendable ` ` abstract https://developer.android.com/reference/java/lang/Appendable` ` https://developer.android.com/reference/java/lang/Appendable#append(char)(char c) `

Appends the specified character to this `Appendable`.
` abstract https://developer.android.com/reference/java/lang/Appendable` ` https://developer.android.com/reference/java/lang/Appendable#append(java.lang.CharSequence,%20int,%20int)(https://developer.android.com/reference/java/lang/CharSequence csq, int start, int end) `

Appends a subsequence of the specified character sequence to this `Appendable`.
` abstract https://developer.android.com/reference/java/lang/Appendable` ` https://developer.android.com/reference/java/lang/Appendable#append(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence csq) `

Appends the specified character sequence to this `Appendable`.
From interface ` https://developer.android.com/reference/java/io/Closeable ` ` abstract void` ` https://developer.android.com/reference/java/io/Closeable#close()() `

Closes this stream and releases any system resources associated with it.
From interface ` https://developer.android.com/reference/java/io/Flushable ` ` abstract void` ` https://developer.android.com/reference/java/io/Flushable#flush()() `

Flushes this stream by writing any buffered output to the underlying stream.
From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.

## Public constructors

### FileWriter

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (File file)
```

Constructs a `FileWriter` given the `File` to write, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset())

<br />

| Parameters ||
|---|---|
| `file` | `File`: the `File` to write. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |

### FileWriter

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (File file, 
                boolean append)
```

Constructs a `FileWriter` given the `File` to write and a boolean indicating whether to append the data written, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()).

<br />

| Parameters ||
|---|---|
| `file` | `File`: the `File` to write <br /> |
| `append` | `boolean`: if `true`, then bytes will be written to the end of the file rather than the beginning <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |

### FileWriter

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (File file, 
                Charset charset)
```

Constructs a `FileWriter` given the `File` to write and [charset](https://developer.android.com/reference/java/nio/charset/Charset).

<br />

| Parameters ||
|---|---|
| `file` | `File`: the `File` to write <br /> |
| `charset` | `Charset`: the [charset](https://developer.android.com/reference/java/nio/charset/Charset) <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |

### FileWriter

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (File file, 
                Charset charset, 
                boolean append)
```

Constructs a `FileWriter` given the `File` to write, [charset](https://developer.android.com/reference/java/nio/charset/Charset) and a boolean indicating whether to append the data written.

<br />

| Parameters ||
|---|---|
| `file` | `File`: the `File` to write <br /> |
| `charset` | `Charset`: the [charset](https://developer.android.com/reference/java/nio/charset/Charset) <br /> |
| `append` | `boolean`: a boolean. If `true`, the writer will write the data to the end of the file rather than the beginning. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |

### FileWriter

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (FileDescriptor fd)
```

Constructs a `FileWriter` given a file descriptor, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()).

<br />

| Parameters ||
|---|---|
| `fd` | `FileDescriptor`: the `FileDescriptor` to write. <br /> |

### FileWriter

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (String fileName)
```

Constructs a `FileWriter` given a file name, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset())

<br />

| Parameters ||
|---|---|
| `fileName` | `String`: String The system-dependent filename. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |

### FileWriter

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (String fileName, 
                boolean append)
```

Constructs a `FileWriter` given a file name and a boolean indicating whether to append the data written, using the platform's [default charset](https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()).

<br />

| Parameters ||
|---|---|
| `fileName` | `String`: String The system-dependent filename. <br /> |
| `append` | `boolean`: boolean if `true`, then data will be written to the end of the file rather than the beginning. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |

### FileWriter

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (String fileName, 
                Charset charset)
```

Constructs a `FileWriter` given a file name and [charset](https://developer.android.com/reference/java/nio/charset/Charset).

<br />

| Parameters ||
|---|---|
| `fileName` | `String`: the name of the file to write <br /> |
| `charset` | `Charset`: the [charset](https://developer.android.com/reference/java/nio/charset/Charset) <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |

### FileWriter

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FileWriter (String fileName, 
                Charset charset, 
                boolean append)
```

Constructs a `FileWriter` given a file name, [charset](https://developer.android.com/reference/java/nio/charset/Charset) and a boolean indicating whether to append the data written.

<br />

| Parameters ||
|---|---|
| `fileName` | `String`: the name of the file to write <br /> |
| `charset` | `Charset`: the [charset](https://developer.android.com/reference/java/nio/charset/Charset) <br /> |
| `append` | `boolean`: a boolean. If `true`, the writer will write the data to the end of the file rather than the beginning. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |