行业资讯 2025年08月6日
0 收藏 0 点赞 156 浏览 2074 个字
摘要 :

文章目录 1.使用Files.readString() – Java 11 2.使用Files.lines() – Java 8 3.使用Files.readAllBytes() – Java 7 4.使用BufferedReader – J……




  • 1.使用Files.readString() – Java 11
  • 2.使用Files.lines() – Java 8
  • 3.使用Files.readAllBytes() – Java 7
  • 4.使用BufferedReader – Java 6
  • 5.使用Apache Commons IO的FileUtils
  • 6.使用Guava的Files

在Java中将文件读取字符串有多种方法:

1.使用Files.readString() – Java 11

在Java 11中引入了readString()方法,它只需要一行代码,就可以使用UTF-8字符集将文件内容读取为字符串。

如果在读取操作期间出现任何错误,此方法会确保文件被正确关闭。 如果文件非常大,例如大于2GB,它会抛出OutOfMemoryError

Path filePath = Path.of(\"c:/temp/demo.txt\");
String content = Files.readString(fileName);

2.使用Files.lines() – Java 8

lines()方法将文件的所有行读入一个Stream中。Stream在消耗时才懒惰地填充。

  • 文件中的字节会使用指定的字符集解码为字符。
  • 返回的Stream包含对打开文件的引用。通过关闭流来关闭文件。
  • 在读取过程中不应修改文件内容,否则结果未定义。
Path filePath = Path.of(\"c:/temp/demo.txt\");
StringBuilder contentBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
  stream.forEach(s -> contentBuilder.append(s).append(\"\\n\"));
} catch (IOException e) {
  //handle exception
}
String fileContent = contentBuilder.toString();

3.使用Files.readAllBytes() – Java 7

readAllBytes()方法将文件的所有字节读入一个字节数组中。不要使用此方法来读取大型文件。

此方法确保在读取所有字节或发生I/O错误或其他运行时异常时关闭文件。读取所有字节后,将这些字节传递给String类构造函数以创建一个新的字符串。

Path filePath = Path.of(\"c:/temp/demo.txt\");
String fileContent = \"\";
try {
    byte[] bytes = Files.readAllBytes(Paths.get(filePath));
    fileContent = new String (bytes);
} catch (IOException e) {
    //handle exception
}

4.使用BufferedReader – Java 6

如果您仍然不使用Java 7或更高版本,那么可以使用BufferedReader类。其readLine()方法逐行读取文件并返回内容。

Path filePath = Path.of(\"c:/temp/demo.txt\");
String fileContent = \"\";
StringBuilder contentBuilder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
    String sCurrentLine;
    while ((sCurrentLine = br.readLine()) != null)
    {
        contentBuilder.append(sCurrentLine).append(\"\\n\");
    }
} catch (IOException e) {
    e.printStackTrace();
}
fileContent = contentBuilder.toString();

5.使用Apache Commons IO的FileUtils

可以使用Apache Commons IO库提供的实用类。FileUtils.readFileToString()是将整个文件读取为字符串的绝佳方式,只需一条语句。

File file = new File(\"c:/temp/demo.txt\");
String content = FileUtils.readFileToString(file, \"UTF-8\");

6.使用Guava的Files

Guava还提供了Files类,可以在一条语句中读取文件内容。

File file = new File(\"c:/temp/demo.txt\");
String content = com.google.common.io.Files.asCharSource(file, Charsets.UTF_8).read();

可以根据您的需求使用上述任何方法将文件读取为字符串。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/9459.html

管理员

相关推荐
2025-08-06

文章目录 一、Reader 接口概述 1.1 什么是 Reader 接口? 1.2 Reader 与 InputStream 的区别 1.3 …

988
2025-08-06

文章目录 一、事件溯源 (一)核心概念 (二)Kafka与Golang的优势 (三)完整代码实现 二、命令…

465
2025-08-06

文章目录 一、证明GC期间执行native函数的线程仍在运行 二、native线程操作Java对象的影响及处理方…

348
2025-08-06

文章目录 一、事务基础概念 二、MyBatis事务管理机制 (一)JDBC原生事务管理(JdbcTransaction)…

456
2025-08-06

文章目录 一、SnowFlake算法核心原理 二、SnowFlake算法工作流程详解 三、SnowFlake算法的Java代码…

517
2025-08-06

文章目录 一、本地Jar包的加载操作 二、本地Class的加载方法 三、远程Jar包的加载方式 你知道Groo…

832
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号