Java IO Tutorial

Java – How to save a String to a File

In Java, there are many ways to write a String to a File.

1. Java 11 – Files.writeString

Finally, a new method added in java.nio to save a String into a File easily.

StringToFileJava11.java

package com.mkyong;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class StringToFileJava11 {

    public static void main(String[] args) {

        String content = "Hello World \r\nJava!\r\n";
        String path = "c:\\projects\\app.log";

        try {

            // Java 11 , default StandardCharsets.UTF_8
            Files.writeString(Paths.get(path), content);

            // encoding
            // Files.writeString(Paths.get(path), content, StandardCharsets.US_ASCII);

            // extra options
            // Files.writeString(Paths.get(path), content, 
			//		StandardOpenOption.CREATE, StandardOpenOption.APPEND);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
c:\\projects\\app.log

Hello World 
Java!

2. Java 7 – Files.write

StringToFileJava7.java

package com.mkyong;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class StringToFileJava7 {

    public static void main(String[] args) {

        String content = "Hello World \r\nJava!\r\n";
        String path = "c:\\projects\\app.log";
		
        try {

            // Java 7
            Files.write(Paths.get(path), content.getBytes());

            // encoding
            // Files.write(Paths.get(path), content.getBytes(StandardCharsets.UTF_8));

            // extra options
            // Files.write(Paths.get(path), content.getBytes(), 
			//		StandardOpenOption.CREATE, StandardOpenOption.APPEND);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output

c:\\projects\\app.log

Hello World 
Java!

3. Apache Commons IO

pom.xml

	<dependency>
		<groupId>commons-io</groupId>
		<artifactId>commons-io</artifactId>
		<version>2.6</version>
	</dependency>
CommonsIOExample.java

package com.mkyong;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class CommonsIOExample {

    public static void main(String[] args) {

        String content = "Hello World \r\nJava!\r\n";
        String path = "c:\\projects\\app2.log";

        try {
            FileUtils.writeStringToFile(new File(path), content, StandardCharsets.UTF_8);

            // append
            // FileUtils.writeStringToFile(new File(path), content, StandardCharsets.UTF_8, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

4. BufferedWriter

4.1 In the old days:

BufferedWriterExample.java

package com.mkyong;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterExample {

    public static void main(String[] args) {

        String content = "Hello World \r\nJava!\r\n";
        String path = "c:\\projects\\app.log";

        try (FileWriter writer = new FileWriter(path);
              BufferedWriter bw = new BufferedWriter(writer)) {

            bw.write(content);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

4.2 Or like this, close all resources manually 🙂

BufferedWriterExampleBeforeJava7.java

package com.mkyong;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterExampleBeforeJava7 {

    public static void main(String[] args) {

        String content = "Hello World \r\nJava!\r\n";
        String path = "c:\\projects\\app.log";

        BufferedWriter bw = null;
        FileWriter fw = null;

        try {

            fw = new FileWriter(path);
            bw = new BufferedWriter(fw);
            
            bw.write(content);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    bw.close();

                if (fw != null)
                    fw.close();

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

}

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
eHowToNow
4 years ago

Doing good job.. I’m following your from 2012. Really Nice website