# How to write files

**URL:** https://discourse.processing.org/t/how-to-write-files/20155
**Category:** Coding Questions
**Created:** [April 25, 2020, 11:03pm UTC](https://discourse.processing.org/t/how-to-write-files/20155 "2020-04-25T23:03:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)
#### Post date: [April 25, 2020, 11:03pm UTC](https://discourse.processing.org/t/how-to-write-files/20155/1 "2020-04-25T23:03:21Z")

</div>

So I have this code:

```auto
import java.io.FileWriter;

FileWriter writeFile = new FileWriter("file.txt");

void setup()
{
  noLoop();
}

void draw() 
{
    writeFile.write("Hello World!");
    writeFile.close();
}

```

But the entire thing is giving me an error. It says that there is a `Unhandelled exception type IOExcpetion`? I am new to files in Processing, and I don’t know what I am doing wrong here. All I want to do is to create and write a file. Any help appreciated. Thanks.

---

<div class="post-metadata">

### Author: ![KevinWho](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@KevinWho](https://discourse.processing.org/u/KevinWho)
#### Post date: [April 25, 2020, 11:21pm UTC](https://discourse.processing.org/t/how-to-write-files/20155/2 "2020-04-25T23:21:11Z")

</div>

You can use a PrintWriter.

Example:

```auto
PrintWriter output;
output = createWriter("file.txt");
output.println("Hello World!");
output.flush();
output.close();

```

You do need to have both flush, and then close

---

<div class="post-metadata">

### Author: ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)
#### Post date: [April 25, 2020, 11:42pm UTC](https://discourse.processing.org/t/how-to-write-files/20155/3 "2020-04-25T23:42:07Z")

</div>

Oh, okay. I’m not familiar with PrintWirter though. How do you read the file?

---

<div class="post-metadata">

### Author: ![KevinWho](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@KevinWho](https://discourse.processing.org/u/KevinWho)
#### Post date: [April 26, 2020, 12:12am UTC](https://discourse.processing.org/t/how-to-write-files/20155/4 "2020-04-26T00:12:41Z")

</div>

```auto
 BufferedReader reader = createReader("file.txt");    

reader.readLine();
 //each time this is called it reads the next line of the file. Returns a String.

```

---

<div class="post-metadata">

### Author: ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)
#### Post date: [April 27, 2020, 12:17am UTC](https://discourse.processing.org/t/how-to-write-files/20155/5 "2020-04-27T00:17:22Z")

</div>

Oh and quick question, how do I append input to a file instead of completely overwriting it?

---

<div class="post-metadata">

### Author: ![KevinWho](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@KevinWho](https://discourse.processing.org/u/KevinWho)
#### Post date: [April 27, 2020, 12:31am UTC](https://discourse.processing.org/t/how-to-write-files/20155/6 "2020-04-27T00:31:21Z")

</div>

read the file, and then add new lines. Also if you want to get the length of the file it seems you have to use the other method to read it. I am using loadStrings. This returns a String[]. Using the method I have made below, you can just add lines to files.

```auto
void appendToFile(String str, String file) {
  String[] data = loadStrings(file);
  PrintWriter output = createWriter(file);

  for (int i=0; i < data.length; i++)
  {
    output.println(data[i]);
  }
  output.println(str);
  output.flush();
  output.close();
}

```
