# How to create and write to a file in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 6, 2019, updated Oct 14, 2022. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-create-and-write-file

> A short tutorial to learn how to create a new file and write data into it using Java.

In this article, you'll **learn how to [create a new text file](https://attacomsian.com/blog/java-create-file) and write data** into it using Java.

## Write to a file using `Files.write()`

If you are using Java 8 or higher, use Java New I/O API `Files.write()` static method to create and write to a file. It is much cleaner and easier to use than the legacy I/O API. 

Here is an example:

```java
try {
    // data to write
    List<String> contents = Arrays.asList("Hey, there!", "What's up?");

    // write data
    Files.write(Paths.get("output.txt"), contents);

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

The above code will create a new file if it does not exist and write to it after truncating any existing data. If you only want to **append data to an existing file**, do the following:

```java
Files.write(Paths.get("output.txt"), contents, StandardOpenOption.APPEND);
```

The above **code will through an exception** if the file doesn't exist. To avoid the exception, you can combine both create and append modes:

```java
Files.write(Paths.get("output.txt"), contents, StandardOpenOption.CREATE, 
        StandardOpenOption.APPEND);
```

If you want to **specify an encoding scheme** other than the default operating system character encoding, do the following:

```java
Files.write(Paths.get("output.txt"), contents, 
        StandardCharsets.UTF_8,
        StandardOpenOption.CREATE);
```

If you want to **write data as bytes**, do the following:

```java
byte[] bytes = {65, 66, 67, 68, 69, 70};
Files.write(Paths.get("output.txt"), bytes);

// write 'ABCDEF' to file 
```

Read [how to read and write files using the Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api) guide for more examples. 

## Write to a file using `BufferedWriter`

You can use the new I/O API (NIO) `Files.newBufferedWriter()` static method to create a new instance of `BufferedWriter`. Here is an example:

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

    // write text to file
    bw.write("Hey, there!");
    bw.newLine();
    bw.write("What's up?");

    // close the writer
    bw.close();

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

The above code will create a new text file called `output.txt` with the following contents:

```bash
Hey, there!
What's up?
```

If you want to append data to a file only, do the following while creating `BufferedWriter`:

```java
BufferedWriter bw = Files.newBufferedWriter(Paths.get("output.txt"), 
        StandardOpenOption.APPEND);
```

## 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)
