# How to save a string to a text file using Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 7, 2019. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-save-string-to-text-file

> A quick tutorial to learn how to save a String to a text file in Java.

In this short article, you'll **learn how to save a string to a text file** in Java.

## Using `Files.writeString()` Method

In Java 11, a new static method called `writeString()` was added to the `Files` class to easily write a string to a file. Here is how you can use it:

```java
try {
    // write string to a file
    Files.writeString(Path.of("output.txt"), "Hey, there!");

} catch (IOException ex) {
    ex.printStackTrace();
}
```

To explicitly **specify a character encoding**, you can do the following:

```java
try {
    // write string to a file
    Files.writeString(Path.of("output.txt"), "Hey, there!",
            StandardCharsets.ISO_8859_1);

} catch (IOException ex) {
    ex.printStackTrace();
}
```

To **create a file if it doesn't already exist** or append the string to an existing one, you can pass optional [file open options](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/StandardOpenOption.html):

```java
try {
    // write string to a file
    Files.writeString(Path.of("output.txt"), "Hey, there!",
            StandardCharsets.ISO_8859_1,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND);

} catch (IOException ex) {
    ex.printStackTrace();
}
```

## Using `Files.write()` Method

For Java 7 or higher, you can use the `Files.write()` method to write a string to a file as shown below:

```java
try {
    // write string to a file
    Files.write(Paths.get("output.txt"), "Hey, there!".getBytes());

} catch (IOException ex) {
    ex.printStackTrace();
}
```

To **specify a different character encoding** other than the default UTF-8, you can do the following:

```java
try {
    // create a string list
    List<String> contents = Collections.singletonList("Hey, there!");
    
    // write string to a file
    Files.write(Paths.get("output.txt"), contents,
            StandardCharsets.UTF_16);

} catch (IOException ex) {
    ex.printStackTrace();
}
```

To **create a non-existing file** or append the string to an existing one, use the following code snippet:

```java
try {
    // create a string list
    List<String> contents = Collections.singletonList("Hey, there!");

    // write string to a file
    Files.write(Paths.get("output.txt"), contents,
            StandardCharsets.UTF_16,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND);

} catch (IOException ex) {
    ex.printStackTrace();
}
```

## Using `BufferedWriter` Class

`BufferedWriter` is another class that you can use to write a string to a text file. Here is an example:

```java
try {
    // create a writer
    BufferedWriter bw = Files.newBufferedWriter(Paths.get("output.txt"));

    // write string to file
    bw.write("Hey, there!");

    // close the writer
    bw.close();

} catch (IOException ex) {
    ex.printStackTrace();
}
```

`Files.newBufferedWriter()` also accepts an optional character encoding:

```java
try {
    // create a writer
    BufferedWriter bw = Files.newBufferedWriter(Paths.get("output.txt"),
            StandardCharsets.UTF_8);

    // write string to file
    bw.write("Hey, there!");

    // close the writer
    bw.close();

} catch (IOException ex) {
    ex.printStackTrace();
}
```

To **create a file if it doesn't exist** or append the string to an existing one, you can also pass file opening options:

```java
try {
    // create a writer
    BufferedWriter bw = Files.newBufferedWriter(Paths.get("output.txt"),
            StandardCharsets.UTF_8,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND);

    // write string to file
    bw.write("Hey, there!");

    // close the writer
    bw.close();

} catch (IOException ex) {
    ex.printStackTrace();
}
```

## Further Reading

You may be interested in other Java I/O articles:

* [Reading and Writing Files in Java](https://attacomsian.com/blog/java-read-write-files)
* [How to Read and Write Text Files in Java](https://attacomsian.com/blog/java-read-write-text-files)
* [How to Read and Write Binary Files in Java](https://attacomsian.com/blog/java-read-write-binary-files)
* [Reading and Writing Files using Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api)
* [How to read a file line by line in Java](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java)
