---
title: "How to create a file and write to a file in Java?"  
description: "How to create a file and write to a file in Java?"  
author: "Anonymous User"  
published: 2015-07-16  
updated: 2015-07-17  
canonical: https://www.mindstick.com/forum/23346/how-to-create-a-file-and-write-to-a-file-in-java  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# How to create a file and write to a file in Java?

What's the simplest way to create and write to a ([text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions)) [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Anonymous User

```
Writer writer = null;try {    writer = new BufferedWriter(new OutputStreamWriter(          new FileOutputStream("filename.txt"), "utf-8"));    writer.write("Something");} catch (IOException ex) {  // report} finally {   try {writer.close();} catch (Exception ex) {/*ignore*/}}
```

\
AND :

```
public class Program {    public static void main(String[] args) {        String text = "Hello world";        BufferedWriter output = null;        try {            File file = new File("example.txt");            output = new BufferedWriter(new FileWriter(file));            output.write(text);        } catch ( IOException e ) {            e.printStackTrace();        } finally {            if ( output != null ) output.close();        }    }}
```


---

Original Source: https://www.mindstick.com/forum/23346/how-to-create-a-file-and-write-to-a-file-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
