Java –如何将字符串保存到文件

在Java中,有很多方法可以将String写入文件。

1. Java 11 – Files.writeString

最后,在java.nio添加了一个新方法,可以轻松地将字符串保存到File

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();
        }

    }

}

输出量

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在过去:

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或者像这样,手动关闭所有资源resources

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();
            }
        }
    }

}

参考文献

翻译自: https://mkyong.com/java/java-how-to-save-a-string-to-a-file/

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值