用Java读取纯文本文件 [英] Reading a plain text file in Java

查看:148
本文介绍了用Java读取纯文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来有不同的方式来读取和写入Java文件的数据。

我想从文件中读取ASCII数据。什么是可能的方式和它们的区别?
解析方案

ASCII是一个TEXT文件,所以你可以使用读者阅读。 Java还支持使用 InputStream 读取二进制文件。如果正在读取的文件很大,那么您可能需要使用 BufferedReader FileReader 之上以提高读取性能。



通过这篇文章了解如何使用一个读者



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



在Java 7中



新的字符串(Files.readAllBytes(...))Files.readAllLines (...)



在Java 8中

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


It seems there are different ways to read and write data of files in Java.

I want to read ASCII data from a file. What are the possible ways and their differences?

解决方案

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.

Go through this article on how to use a Reader

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

In Java 7:

new String(Files.readAllBytes(...)) or Files.readAllLines(...)

In Java 8:

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

这篇关于用Java读取纯文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆