Store String inside a File using Java

0 votes
I have some value inside a String variable by the name 'text'. How can I save the value from this variable to a text file using JAVA ?
May 3, 2018 in Java by developer_1
• 3,320 points

edited May 3, 2018 by developer_1 1,056 views

2 answers to this question.

0 votes
If you're simply outputting text, 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.

If you are using Java 7 or later, you can use the "try-with-resources statement" which will automatically close your PrintStream when you are done with it (ie exit the block) like so:

try (PrintWriter out = new PrintWriter("filename.txt")) {
    out.println(text);
}

You will still need to explicitly throw the java.io.FileNotFoundException as before.
answered May 3, 2018 by geek
0 votes

We can use Apache Commons IO. It has some great methods to do this.

static void writeStringToFile(File file, String data) 
FileUtils.writeStringToFile(new File("test.txt"), "Hello File");
answered Jul 20, 2018 by Sushmita
• 6,910 points

Related Questions In Java

0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 13, 2020 in Java by Roshni
• 10,520 points
3,465 views
0 votes
1 answer

How do I save a String to a text file using Java?

If you're simply outputting text, rather than ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,910 points

edited Jul 6, 2023 by Khan Sarfaraz 1,759 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
927 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,910 points
2,275 views
0 votes
3 answers

Adding text to a file using Java

try { final Path ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,910 points
1,097 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,910 points
1,634 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by Parth
• 4,630 points
754 views
0 votes
3 answers

How to convert File to Byte[] in Java?

import java.io.File; import java.nio.file.Files; File file; // ...(file is initialised)... byte[] ...READ MORE

answered Aug 15, 2018 in Java by samarth295
• 2,220 points
1,929 views
0 votes
2 answers

How do I rename a file using Java

// File (or directory) with old name File ...READ MORE

answered Oct 5, 2018 in Java by Sushmita
• 6,910 points
559 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,910 points
3,529 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP