编程语言
首页 > 编程语言> > 如何从Java文件中读取具有所选大小的流的文本?

如何从Java文件中读取具有所选大小的流的文本?

作者:互联网

所以我可以像这样用Java读取文本文件:

File file = new File("C:/text.txt");
Scanner scanner = new Scanner(file,"utf-8");
while (scanner.hasNext()) {
    scanner.nextLine();
    // rest of the code...

我不确定100%,但是我相信这会加载整个文件,然后逐行读取文本.

假设您有一个很大的文本文件,其中一行可能太长.

如何从文件中读取文本,每次说50个字符?我想要的是在每次迭代中我应该得到50个字符,而不是更多.

解决方法:

A simple text scanner which can parse primitive types and strings
using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern,
which by default matches whitespace. The resulting tokens may then be
converted into values of different types using the various next
methods.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

这是答案:Faster way to read file

标签:java-io,java
来源: https://codeday.me/bug/20191121/2054795.html