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

拼搏现实的明天。 2023-02-15 05:18 87阅读 0赞

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

1. Java 11 – Files.writeString

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

StringToFileJava11.java

  1. package com.mkyong;
  2. import java.io.IOException;
  3. import java.nio.charset.StandardCharsets;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.nio.file.StandardOpenOption;
  7. public class StringToFileJava11 {
  8. public static void main(String[] args) {
  9. String content = "Hello World \r\nJava!\r\n";
  10. String path = "c:\\projects\\app.log";
  11. try {
  12. // Java 11 , default StandardCharsets.UTF_8
  13. Files.writeString(Paths.get(path), content);
  14. // encoding
  15. // Files.writeString(Paths.get(path), content, StandardCharsets.US_ASCII);
  16. // extra options
  17. // Files.writeString(Paths.get(path), content,
  18. // StandardOpenOption.CREATE, StandardOpenOption.APPEND);
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }

c:\\projects\\app.log

  1. Hello World
  2. Java!

2. Java 7 – Files.write

StringToFileJava7.java

  1. package com.mkyong;
  2. import java.io.IOException;
  3. import java.nio.charset.StandardCharsets;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.nio.file.StandardOpenOption;
  7. public class StringToFileJava7 {
  8. public static void main(String[] args) {
  9. String content = "Hello World \r\nJava!\r\n";
  10. String path = "c:\\projects\\app.log";
  11. try {
  12. // Java 7
  13. Files.write(Paths.get(path), content.getBytes());
  14. // encoding
  15. // Files.write(Paths.get(path), content.getBytes(StandardCharsets.UTF_8));
  16. // extra options
  17. // Files.write(Paths.get(path), content.getBytes(),
  18. // StandardOpenOption.CREATE, StandardOpenOption.APPEND);
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }

输出量

c:\\projects\\app.log

  1. Hello World
  2. Java!

3. Apache Commons IO

pom.xml

  1. <dependency>
  2. <groupId>commons-io</groupId>
  3. <artifactId>commons-io</artifactId>
  4. <version>2.6</version>
  5. </dependency>

CommonsIOExample.java

  1. package com.mkyong;
  2. import org.apache.commons.io.FileUtils;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.nio.charset.StandardCharsets;
  6. public class CommonsIOExample {
  7. public static void main(String[] args) {
  8. String content = "Hello World \r\nJava!\r\n";
  9. String path = "c:\\projects\\app2.log";
  10. try {
  11. FileUtils.writeStringToFile(new File(path), content, StandardCharsets.UTF_8);
  12. // append
  13. // FileUtils.writeStringToFile(new File(path), content, StandardCharsets.UTF_8, true);
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }

4. BufferedWriter

4.1在过去:

BufferedWriterExample.java

  1. package com.mkyong;
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. public class BufferedWriterExample {
  6. public static void main(String[] args) {
  7. String content = "Hello World \r\nJava!\r\n";
  8. String path = "c:\\projects\\app.log";
  9. try (FileWriter writer = new FileWriter(path);
  10. BufferedWriter bw = new BufferedWriter(writer)) {
  11. bw.write(content);
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. }

4.2或者像这样,手动关闭所有资源resources

BufferedWriterExampleBeforeJava7.java

  1. package com.mkyong;
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. public class BufferedWriterExampleBeforeJava7 {
  6. public static void main(String[] args) {
  7. String content = "Hello World \r\nJava!\r\n";
  8. String path = "c:\\projects\\app.log";
  9. BufferedWriter bw = null;
  10. FileWriter fw = null;
  11. try {
  12. fw = new FileWriter(path);
  13. bw = new BufferedWriter(fw);
  14. bw.write(content);
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. } finally {
  18. try {
  19. if (bw != null)
  20. bw.close();
  21. if (fw != null)
  22. fw.close();
  23. } catch (IOException ex) {
  24. ex.printStackTrace();
  25. }
  26. }
  27. }
  28. }

参考文献

  • 文件Java文档
  • Java教程–读取,写入和创建文件
  • BufferedWriter JavaDocs
  • Java –如何将文本追加到文件
  • Java –如何创建和写入文件

标签: 文件 Java 11 Java 7 java.io java.nio 写入文件

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

发表评论

表情:
评论列表 (有 0 条评论,87人围观)

还没有评论,来说两句吧...

相关阅读