Java How to save a String to a File

摘要: In Java, there are many ways to write 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();

上一篇: Java Convert File to String
下一篇: FastJson Convert Java objects to / from JSON
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号