有没有办法告诉 java BufferedReader 读取到某个点

发布于 2024-12-17 05:29:28 字数 166 浏览 3 评论 0原文

我正在使用 Java BufferedReader 并使用 bufferedReader.readline() 逐行读取文件。

有没有一种方法可以让我知道该行有这么多文本,并且不要阅读超出的文本。

换句话说,是否已经存在可以看到行内容大小然后开始阅读的东西。那么它会读取到提供的大小吗?

I am using Java BufferedReader and reading file line by line using bufferedReader.readline().

Is there a way by which I can tell that the line has this much text and Don't read it beyond that.

In other words is there already existing something which sees the line content size and then start reading. Then it would be reading upto the size which is provided?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难以启齿的温柔 2024-12-24 05:29:28

这个< /a> 你的意思是?

public int read(char[] buf,
整数偏移量,
整数计数)

其中:

buf - 应存储读取的字符的数组,偏移量 -
数组中开始存储字符的偏移量,计数 - 请求的
要读取的字符数

Is this what you mean?

public int read(char[] buf,
int offset,
int count)

where:

buf - The array into which the chars read should be stored, offset -
The offset into the array to start storing chars, count - The requested
number of chars to read

初熏 2024-12-24 05:29:28

您可以使用 read(char[] buffer,int offset,int length) 来实现此目的。

You can use read(char[] buffer,int offet,int length) for this purpose.

笑着哭最痛 2024-12-24 05:29:28

如果您知道一行的大小,则可以使用 BufferedReader.read(char[] cbuf, int off, int len) 读取您想要的内容并使用 BufferedReader.skip(long n ) 跳过其余部分。


您可以阅读您的行,然后将其截断为您期望的长度。

String line = bufferedReader.readLine();
String truncatedLine = line.substring(0, expectedLength);

If you know the size of a line than you can use BufferedReader.read(char[] cbuf, int off, int len) to read what you want and use BufferedReader.skip(long n) to skip the rest.

OR
You can read your line and then truncate it to the length you are expecting.

String line = bufferedReader.readLine();
String truncatedLine = line.substring(0, expectedLength);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文