---
title: "How can I save a String in a text file using Java?"  
description: "How can I save a String in a text file using Java?"  
author: "Anonymous User"  
published: 2013-10-04  
updated: 2013-10-04  
canonical: https://www.mindstick.com/forum/1581/how-can-i-save-a-string-in-a-text-file-using-java  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# How can I save a String in a text file using Java?

How can I save a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) in a [text file](https://www.mindstick.com/forum/12828/converting-a-text-file-to-an-array-in-java)?

## Replies

### Reply by Anonymous User

If you're simply outputting [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions), rather than any binary data, the following will work:

```
PrintWriter out = new PrintWriter("filename.txt");
```

Then, write your String to it, just like you would to any output stream:

```
out.println(text);
```

You'll need exception handling, as ever. Be sure to call out.close() when you've finished writing.

Edit: further simplified per Jonik's comments.


---

Original Source: https://www.mindstick.com/forum/1581/how-can-i-save-a-string-in-a-text-file-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
