且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Java读取纯文本文件

更新时间:2023-01-18 19:26:30

ASCII 是一个文本文件,所以你可以使用 Readers 用于阅读.Java 还支持使用 InputStreams 从二进制文件中读取.如果正在读取的文件很大,那么您需要使用 BufferedReaderFileReader 以提高读取性能.

ASCII is a TEXT file so you would use Readers for reading. Java also supports reading from a binary file using InputStreams. If the files being read are huge then you would want to use a BufferedReader on top of a FileReader to improve read performance.

阅读这篇文章,了解如何使用读者

我还建议您下载并阅读这本精彩(但免费)的书,名为思考在 Java 中

I'd also recommend you download and read this wonderful (yet free) book called Thinking In Java

在 Java 7 中:

new String(Files.readAllBytes(...))

(文档)

Files.readAllLines(...)

(文档)

在 Java 8 中:

Files.lines(..).forEach(...)

(文档)