Java –如何将字符串保存到文件 您所在的位置:网站首页 将字符串写进文件里 Java –如何将字符串保存到文件

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

2024-07-08 14:43| 来源: 网络整理| 查看: 265

在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 commons-io commons-io 2.6 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(); } } } } 参考文献 文件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/



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有